简体   繁体   English

如何用str_replace替换多个字符串?

[英]How can I replace multiple Strings with str_replace?

How can I replace multiple strings with str_replace ? 如何用str_replace替换多个字符串?

$oldurl = JFactory::getURI();
$newurl = str_replace('example1', 'replacedwiththis', $oldurl);

but I need that this code works also if the URL contains example2 . 但是我需要该代码在URL包含example2 So I need a code that changes both example1 and example2 with replacedwiththis . 所以,我需要改变这两个代码example1example2replacedwiththis

With str_replace() you can define an array of 'needles' to find ( http://php.net/manual/en/function.str-replace.php ). 使用str_replace()可以定义一个“针”数组以进行查找( http://php.net/manual/en/function.str-replace.php )。

So you can do ... 所以你可以做...

$newurl = str_replace(['example1','example2'], 'replacedwiththis', $oldurl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM