Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP String Replace From Array of Values

Tags:

php

I have several strings that will be replaced with the same string...

ie

 $text=str_ireplace('[/VIDEO]','</div>',$text);
 $text=str_ireplace('[/ARTICLE]','</div>',$text);
 $text=str_ireplace('[/IMG]','</div>',$text);

anyway to use an array in lieu of the initial string value?

like image 654
Howard Zoopaloopa Avatar asked Dec 23 '22 00:12

Howard Zoopaloopa


1 Answers

Yes. See the documentation for str_ireplace:

If search and replace are arrays, then str_ireplace() takes a value from each array and uses them to do search and replace on subject. If replace has fewer values than search, then an empty string is used for the rest of replacement values. If search is an array and replace is a string, then this replacement string is used for every value of search.

$text=str_ireplace(array('[/VIDEO]', '[/ARTICLE]', '[/IMG]'),'</div>',$text);
like image 117
Artefacto Avatar answered Dec 24 '22 13:12

Artefacto