如何用 str_replace 替换多个字符串?
How can I replace multiple Strings with str_replace?
如何用 str_replace
替换多个字符串?
$oldurl = JFactory::getURI();
$newurl = str_replace('example1', 'replacedwiththis', $oldurl);
但如果 URL
包含 example2
,我需要此代码也能正常工作。
所以我需要一个代码来改变 example1
和 example2
与 replacedwiththis
.
使用 str_replace()
你可以定义一个 'needles' 的数组来查找 (http://php.net/manual/en/function.str-replace.php).
所以你可以做...
$newurl = str_replace(['example1','example2'], 'replacedwiththis', $oldurl);
如何用 str_replace
替换多个字符串?
$oldurl = JFactory::getURI();
$newurl = str_replace('example1', 'replacedwiththis', $oldurl);
但如果 URL
包含 example2
,我需要此代码也能正常工作。
所以我需要一个代码来改变 example1
和 example2
与 replacedwiththis
.
使用 str_replace()
你可以定义一个 'needles' 的数组来查找 (http://php.net/manual/en/function.str-replace.php).
所以你可以做...
$newurl = str_replace(['example1','example2'], 'replacedwiththis', $oldurl);