I want to link matches for specific words in a sentence. Overall this is easy, and sample code could go like this:
$words = array("Facebook", "Apple");
$text = "Is Facebook's vr hardware better than Apple's current prototype?";
foreach($words as $w) {
$pattern = '/' . $w .'\b/i';
$link = '<a href="/blah/' . strtolower($w) . '">' . $w . '</a>';
$text = preg_replace($pattern, $link, $text);
}
print $text;
However I would like to catch variations of words that have 's
(apostrophe-s).
To do that I need to search for the two possible variations (with and without the 's
), but the outcome also affects what text used in the replacement.
I'm drawing a blank on how to pro-actively used preg_match
and then alter preg_replace
based on the outcome. Any advice appreciated.