I want to check existence of the word 'açilek' in the context. Running this:
$word = 'açilek';
$article='elma and açilek word';
$mat=preg_match('/\b'. $word .'\b/', $article);
var_dump($mat);
Succeeds. This is expected. However, to match the word 'çilek', the code returns False which is not expected:
$word = 'çilek';
$article='elma and çilek word';
$mat=preg_match('/\b'. $word .'\b/', $article);
var_dump($mat); //returns false !!!!
Additionally, it will match this word if it is a part of a word, also unexpected:
$word = 'çilek';
$article='elma and açilek word';
$mat=preg_match('/\b'. $word .'\b/', $article);
var_dump($mat); //returns true !!!!
Why am I seeing this behavior?
You need to use the /u
modifier to make the regex (especially \b
) Unicode-aware:
$mat=preg_match('/\b'. $word .'\b/u', $article);
Otherwise, \b
only considers positions between ASCII alphanumerics and ASCII non-alnums as word boundaries, therefore matching between a
and çilek
but not between
and çilek
.
beware that UTF8 characters patterns/metacharacters are not seen as such by the PCRE engine (and may very well break the matching) if you don't provide the "u" switch, as so :
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
$mat=preg_match('/\b'. $word .'\b/u', $article);