Reputation: 3065
I want to replace all occurrences of a
with 5
. Here is the 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 there is a Unicode (or non-ASCII) letter followed by a
at the end of word. Is there any easy way to fix it?
Upvotes: 3
Views: 81
Reputation: 93036
the problem is that the predefined character class \w
is ASCII based and that does not change, when the u
modifier is used. (See regular-expressions.info, preg is PCRE in the columns)
You can use lookbehind and lookahead to do it:
$content=preg_replace("/(?<!\p{L})a(?!\p{L})/","5",$content);
This will replace "a" if there is not a letter before and not a letter ahead.
\p{L}
: any kind of letter from any language.
Upvotes: 3