php regex and or ( | )

I am experimenting a problem with | (or) and the regex in php.

Here is what I would like to do, for example I have those 3 sentences :

“apple is good to eat”

“an apple a day keeps the doctor away”

“i like to eat apple”

And lets say that i would like to change the word apple by orange, so here is my code :

$text = preg_replace('#^' . $oldWord . ' #', $newWord, $text);
$text = preg_replace('# ' . $oldWord . ' #', $newWord, $text);
$text = preg_replace('# ' . $oldWord . '$#', $newWord, $text);

Of course it works but i haven’t found the right combinaison to do that with only one line of code with the key word | (or).

Do you guys have any suggestion please ? thanks

Leave a Reply


Hire Me
Follow Me!
Most Popular Articles & Pages
Because your vote is Important
Sorry, there are no polls available at the moment.