素材牛VIP会员

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); 
2022年11月25日 综合评分:0 雪女@口月