$result = str_replace(array_keys($replacer), array_values($replacer),$string);

<?php
//stringParser.php
//5、PHP替换标签字符
//
//有时我们需要将字符串、模板标签替换成指定的内容,可以用到下面的函数:
//
function stringParser($string,$replacer){ 
    $result = str_replace(array_keys($replacer), array_values($replacer),$string); 
    return $result; 
}
//使用方法如下:
//
$string = 'The {b}anchor text{/b} is the {b}actual word{/b} or words used {br}to describe the link {br}itself'; 
$replace_array = array('{b}' => '<b>','{/b}' => '</b>','{br}' => '<br />'); 

echo stringParser($string,$replace_array), '<br>';

var_dump($replace_array);
var_dump(array_keys($replace_array));
var_dump(array_values($replace_array));

?>

 

posted @ 2017-11-24 10:06  sky20080101  阅读(112)  评论(0)    收藏  举报