本文介绍了搜索和在PHP5多个/不同的值替换多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个内置的PHP函数与决定什么被替换什么数组替换字符串中的多个值?
Is there an inbuilt PHP function to replace multiple values inside a string with an array that dictates exactly what is replaced with what?
例如:
$searchreplace_array = Array('blah' => 'bleh', 'blarh' => 'blerh');
$string = 'blah blarh bleh bleh blarh';
所得将是:'的Bleh blerh的Bleh的Bleh blerh
And the resulting would be: 'bleh blerh bleh bleh blerh'.
推荐答案
您正在寻找。
You are looking for str_replace()
.
$string = 'blah blarh bleh bleh blarh';
$result = str_replace(
array('blah', 'blarh'),
array('bleh', 'blerh'),
$string
);
//附加提示:
如果你被卡住的关联数组就像在你的榜样,你可以把它分解得那样:
And if you are stuck with an associative array like in your example, you can split it up like that:
$searchReplaceArray = array(
'blah' => 'bleh',
'blarh' => 'blerh'
);
$result = str_replace(
array_keys($searchReplaceArray),
array_values($searchReplaceArray),
$string
);
这篇关于搜索和在PHP5多个/不同的值替换多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!