有时我们需要将字符串、模板标签替换成指定的内容,可以用到下面的函数:
1 function stringParser($string,$replacer){ 2 3 $result = str_replace(array_keys($replacer), array_values($replacer),$string); 4 5 return $result; 6 7 } 8 9 //使用方法如下 10 11 $string = 'The {b}anchor text{/b} is the {b}actual word{/b} or words used 12 {br}to describe the link {br}itself'; 13 14 $replace_array = array('{b}' => '','{/b}' => '','{br}' => '
'); 15 16 17 18 echo stringParser($string,$replace_array);