Match whole words in utf
I want to replace all occurrences a
with 5
. Here's some code that works well:
$content=preg_replace("/\ba\b/","5", $content);
unless I have words like zapΕaΔ
where a
is between non-standard characters or zmarΕa
where is there a Unicode (or non-ASCII) letter followed a
at the end of the word. Is there an easy way to fix it?
source to share
the problem is that the predefined character class is \w
based on ASCII and does not change when the modifier is used u
. (See regular-expressions.info , preg is PCRE in columns)
You can use lookbehind and lookahead to do this:
$content=preg_replace("/(?<!\p{L})a(?!\p{L})/","5",$content);
This will replace "a" if there was no letter before, not forward letter.
\p{L}
: any letter from any language .
source to share