PHP str_replace的用法
1 替换单个字符
<?php
echo str_replace("world","Shanghai","Hello world!");
?>
//结果
Hello Shanghai!
2 多个字符替换多个字符
$result=str_replace(array(1,2,3), array(3,4,5), "good5 golly2 miss3 molly!");
echo $result;
//结果
good5 golly4 miss5 molly!
3 如果碰到 要匹配的是相同的可以采取如下方法
<?php
$pattern = array('/a/', '/a/', '/a/');
$replacement = array('x', 'y', 'z');
$subject = 'abcadeafg';
$res = preg_replace($pattern, $replacement, $subject , 1);
echo $res; // xbcydezfg$pattern = array('//?/', '//?/', '//?/');
$replacement = array('x', 'y', 'z');
$subject = '?b?d?bfg';
$res = preg_replace($pattern, $replacement, $subject , 1);
echo $res; // xbydzbfg?>
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/19100.html