Using PHP, I’m looking to take a piece of text and search through it and replace certain words with another word from the list.
e.g.
Search through the text to find any word in this list:
pretty,beautiful,gorgeous,lovely,attractive,appealing
and then replace this word with another from the same list
(but not selecting the same word).
Hope this makes sense!
thanks in advance.
you could use
preg_replace_callback:to make sure your
$needlearray does not contain characters which have a special meaning in a regular expression, we callpreg_quoteon each item of the array before searching.instead of doing a
do{}while()loop you could also copy the array and remove the matched word (pretty much depends on the actual data: few items → copy&remove, many items → pick one random until it’s different from the match)