Case and accent insensitive searchs in MongoDB and PHP

The problem

Comming from MySQL, programmers gets a little lazy about searching strings. MySQL does all the dirty job for you: search joao and find joao, joão, João, JOÃO and any other variants.

But... when you start using MongoDB, even with his powerfull features, well, you get lost. Let's find a solution.

The Solution

A PHP function using Regular Expression (REGEX) to query MongoDB.

! MongoDB has evolved since this article was written (2013). Consider using MongoDB Text Indexes as a waaaay better solution.