0

i need the following -

if i have a sentence

$str = "i like programming very much";

and i search for a word

$word = "r";

i expect it to return the sentence

"i like *p***r***og***r***aming* *ve***r***y* much"

I wrote the following regex for it, but it sometimes doesn't work.

$str = preg_replace("/([^\s{".preg_quote($word)."}]*?)(".preg_quote($word).")([^\s{".preg_quote($word)."}]*)/siu","<span class='pice1'>$1</span><span class='pice2'>$2</span><span class='pice1'>$3</span>",$str);

Could you tell me what i wrote wrong?

Thanks

UPDATE:

for example it doesn't work when

$str = "ameriabank"; and $word = "ab";

...

Simon
  • 22,637
  • 36
  • 92
  • 121
  • 1
    What exactly are you trying to do? Do you want to highlight words that contain a given sub-string? – Gumbo Aug 27 '10 at 12:23
  • That seems to be the gist of it, and it seems that people commonly want to solve that problem. I think it's been solved before. Search for "highlight" and I'm sure you'll get lots of good answers. – Ian Aug 27 '10 at 12:27
  • in example i've shown what i expect. I need to highlight the whole word if it contains the sub_word, but highlight the sub_word in other way, then other part of word. As in example(see the word programming)... – Simon Aug 27 '10 at 12:29
  • Location: http://stackoverflow.com/questions/2757556/highlight-multiple-keywords-in-search – shamittomar Aug 27 '10 at 12:31
  • @shamittomar they do another thing. i need two types of highlighting in each word which contain keyword. – Simon Aug 27 '10 at 13:02

5 Answers5

3

Why dont't you just use str_replace()? I think it's more simple

$search = "ab";
$word = "ameriabank";
$newstr = "<span class=\"pice1\">".str_replace($search, $word, "</span><span class=\"pice3\">".$search."</span></span class=\"pice1>\")."</span>";
Tokk
  • 4,499
  • 2
  • 31
  • 47
2
$str = "i like programming very much";
$w = "r";
echo preg_replace("/($w)/", "<b>$1</b>", $str);

Output:

i like p<b>r</b>og<b>r</b>amming ve<b>r</b>y much

Answer to the comment: do it in two steps.

$str = "i like programming very much ready tear";
$w = "r";
$str = preg_replace("/\\b((?:\\w+|\\b)$w(\\w+|\\b))\\b/", "<i>$1</i>", $str);
$str = preg_replace("/($w)/", "<b>$1</b>", $str);
echo $str;

output:

i like <i>p<b>r</b>og<b>r</b>amming</i> <i>ve<b>r</b>y</i> much <i><b>r</b>eady</i> <i>tea<b>r</b></i>
Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • what about other parts of word `programming`? i need to highlight them too, but in other way(not bold i mean)... – Simon Aug 27 '10 at 12:37
1

visit highlight multiple keywords in search and be amazed.

Community
  • 1
  • 1
Ian
  • 4,421
  • 1
  • 20
  • 17
1

What about this way :

$str = "i like programming very much";
$word = "r";
$list = explode(' ',$str);
for($i=0; $i<count($list); $i++) {
    if(preg_match("/$word/", $list[$i])) {
        $list[$i] = '<i>'.preg_replace("/$word/siu", "<b>$word</b>", $list[$i]).'</i>';
    }
}
$str = implode(' ',$list);
echo $str,"\n";
Toto
  • 89,455
  • 62
  • 89
  • 125
0
$str = "i like programming very much";
$word = "r";
function highlight($matches)
{
    global $word;
    return '<span class="pice1">'.str_replace($word,'<span class="pice2">'.$word.'</span>',$matches[0]).'</span>';
}
echo $str = preg_replace_callback("/([^\s]*?".preg_quote($word, '/')."[^\s]*)/siu", highlight, $str);

do the job(and it works with foreign languages too)...

Simon
  • 22,637
  • 36
  • 92
  • 121