Andreas Korthaus: suche in strings, inhalt ändern

Beitrag lesen

Hallo!

hier mal ein auszug von der string fett routine:
$array = explode (" ", $searchtext); //suchtxt
      $text = $printit[description]; //beschreibung
      $total = count($array);
          $i = 0;
   while($i < $total){
  $foo = $array[$i];
   $bar = eregi_replace($foo, "<strong>$foo</strong>", $text);
      $text = $bar;
     $i++;
   }
             echo "$bar";

} else {
...

So geht das nicht (ohne weiteres), viel einfacher ist es mit http://php3.de/manual/de/function.preg-replace.php, mit \b definierst Du Wortgrenzen, also reicht folgender Ausdruck:

$text_mit_strong = preg_replace("/\b($suchwort)\b/", "<strong>$1</strong>", $kompletter_text);

Grüße
Andreas