2

Found this function to highlight keywords in a search string in the thread highlight multiple keywords in search

function highlight($text, $words) {
    preg_match_all('~\w+~', $words, $m);
    if(!$m)
        return $text;
    $re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';
    return preg_replace($re, '<b>$0</b>', $text);
}

But it does not work for non-english characters, how can I tweak it to work with e.g. å ä ö ô etc.

Community
  • 1
  • 1
Joseph
  • 1,734
  • 6
  • 29
  • 51
  • I was having exactly the same problem. I could solve it thanks to this question and own [answer](http://stackoverflow.com/a/30260166/1883256). – Pathros Oct 22 '15 at 19:34

2 Answers2

0

Are you sure you are searching for unicode characters? \w Only matches [a-zA-Z0-9_].

For example this matches persian digits :

preg_match( "/[^\x{06F0}-\x{06F9}\x]+/u" , '۱۲۳۴۵۶۷۸۹۰' );

Source : http://php.net/manual/en/function.preg-match.php

Just create a character class with the characters you want to match.

FailedDev
  • 26,680
  • 9
  • 53
  • 73
0

You better do it with JS after the page loads it with needed encoding:

function paint_keys( str_to_replace , words ){
temp_reg = str_to_replace;
if( /\s/g.test( temp_reg ) ) temp_reg = temp_reg.split( " " ); // Breaking our searching keywords into an array;
else return words.split( temp_reg ).join( "<span class='painted'>" + temp_reg + "</span>" );
for( z = 0 ; z < temp_reg.length ; z++ ){
    if( temp_reg[ z ] != "" ) words = words.split( temp_reg[ z ] ).join( "<span class='painted'>" + temp_reg[ z ] + "</span>" );
}
return words;
}

Of course, you can replace the <span class='painted'>" + temp_reg + "</span> to whatever you need.

If you need it to be done by php only - use the principle of breaking the keys to array by explode( "\s" , $words ); - that will give you an array of symbols only and then loop to replace the string for each word in the array.

Michael Sazonov
  • 1,531
  • 11
  • 21