Snickfire
Snickfire

Reputation: 512

Preg_match doesn't find accents

I'm trying to search a word with accents or spanish characters but it doesn't work

if(preg_match('|\b(' . $iText . ')\b|i', $str_content)) {...}

For example if i insert

$iText="desempeño";

or

$iText="Función necesaria" 

and it doesn't work, of course, i post $iText with a form input.

--------------EDIT--------

Well i solved with a array replacing normal characters to ASCII characters:

function prepare($pattern)
{
$replacements =array('À'=>'À', 'à'=>'à', 'Á'=>'Á', 'á'=>'á', 'Â'=>'Â', 'â'=>'â', 'Ã'=>'Ã', 'ã'=>'ã', 'Ä'=>'Ä', 'ä'=>'ä', 'Å'=>'Å', 'å'=>'å', 'Æ'=>'Æ', 'æ'=>'æ', 'Ç'=>'Ç', 'ç'=>'ç', 'Ð'=>'Ð', 'ð'=>'ð', 'È'=>'È', 'è'=>'è', 'É'=>'É', 'é'=>'é', 'Ê'=>'Ê', 'ê'=>'ê', 'Ë'=>'Ë', 'ë'=>'ë', 'Ì'=>'Ì', 'ì'=>'ì', 'Í'=>'Í', 'í'=>'í', 'Î'=>'Î', 'î'=>'î', 'Ï'=>'Ï', 'ï'=>'ï', 'Ñ'=>'Ñ', 'ñ'=>'ñ', 'Ò'=>'Ò', 'ò'=>'ò', 'Ó'=>'Ó', 'ó'=>'ó', 'Ô'=>'Ô', 'ô'=>'ô', 'Õ'=>'Õ', 'õ'=>'õ', 'Ö'=>'Ö', 'ö'=>'ö', 'Ø'=>'Ø', 'ø'=>'ø', 'Œ'=>'Œ', 'œ'=>'œ', 'ß'=>'ß', 'Þ'=>'Þ', 'þ'=>'þ', 'Ù'=>'Ù', 'ù'=>'ù', 'Ú'=>'Ú', 'ú'=>'ú', 'Û'=>'Û', 'û'=>'û', 'Ü'=>'Ü', 'ü'=>'ü', 'Ý'=>'Ý', 'ý'=>'ý', 'Ÿ'=>'Ÿ', 'ÿ'=>'ÿ');
 return str_replace(array_keys($replacements), $replacements, $pattern);  
}

then i put this function in the if :

 if(preg_match('|\b(' . prepare($iText) . ')\b|i', prepare($str_content)))

Upvotes: 2

Views: 766

Answers (1)

You need to add the u (PCRE_UTF8) modifier to your regular expression

u (PCRE_UTF8) This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

Source

Just add it like this..

if(preg_match('|\b(' . $iText . ')\b|ui', $str_content)) {...}
                                     ^--- Here

Upvotes: 1

Related Questions