views:

42

answers:

3

Suppose there are 3 keywords,

I don't want to do this kind of replace 3 times:

a => <b>a</b>

str_replace ( 'a', '<b>a</b>', $str)

Is it possible to do it by one run?

+1  A: 

You can use the strtr (or mb_strtr) function in PHP

$trans = array("hello" => "hi", "said" => "screamed");
echo strtr("hi all, I said hello", $trans);  // prints out "hi all, I screamed hi"
michal kralik
A: 

Use preg_replace() with a backreference:

$text = "foo bar baz";
echo preg_replace('/(a)/', '<b>$1</b>', $text);
Ignacio Vazquez-Abrams
A: 
preg_replace('/(a|e|i|o|u)/', '<b>$1</b>', $string);

have you considered looking into <b> vs <strong> ?

alex