Asad kamran
Asad kamran

Reputation: 440

preg_replace not working for utf-8 Arabic text

I writing a php function to check existence of bad whole words (keep in mind whole word not sub-strings) and also highlight whole words in given string.

function badwordChecherAndHighLighter($str,$replace){
// $replace=1  will  Highlight
// $replace=0  will  Check the existence of any badwords

$result = mysql_query("SELECT settings_badwords_en,settings_badwords_ar FROM settings_badwords WHERE settings_badwords_status=1") or die(mysql_error());

// i dont create an array, may create overhead, so i directly apply in preg_replace

if($replace==1){
while($row = mysql_fetch_row($result))
{
//$str=preg_replace('/'.$row[0].'/i', str_repeat("*",strlen($row[0])), $str);
$str=preg_replace('/\b('.$row[0].'\b)/i',"" .$row[0] . "" , $str);
$str=preg_replace('/\b('.$row[1].'\b)/i',"" .$row[1] . "" , $str);
}
return $str;
}else{

while($row = mysql_fetch_row($result))
{
 if(preg_match('/\b('.$row[0].'\b)/i',$str)) return 1;
 if(preg_match('/\b('.$row[1].'\b)/i',$str)) return 1;
}
return 0; 
}
}

// $row[1] conatin Arabic bad Words, and $row[0] contain English bad words.

This function gives correct results on Windows OS, WAMP5 1.7.3 for both Arabic and English.

But on Web Server It only works for English words, and not for Arabic.

So if Arabic text is given to this function , it is unable to check existence of any badword, and also unable to highlight arabic word.

I searched and try many options including \u but no error, no success.

So please help.

Upvotes: 0

Views: 3058

Answers (1)

turbod
turbod

Reputation: 1988

The \b is not compatible the utf8 characters. Try this:

preg_match('/(?<=^|[^\p{L}])' . preg_quote($utf8word,'/') . '(?=[^\p{L}]|$)/ui',$utf8string);

Upvotes: 4

Related Questions