Search code examples
phpjoomlastr-replace

How can I replace multiple Strings with str_replace?


How can I replace multiple strings with str_replace?

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

but I need that this code works also if the URL contains example2. So I need a code that changes both example1 and example2 with replacedwiththis.


Solution

  • With str_replace() you can define an array of 'needles' to find (http://php.net/manual/en/function.str-replace.php).

    So you can do ...

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