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?

+3


source to share


2 answers


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 .

+3


source


$content=preg_replace("/\ba\b/u","5",$content);

      



0


source







All Articles