Add links to specific words within span tag in PHP
- by dazhall
I have a list of words that I'd like to add a link to, I can do this fairly easily using preg_match_all and preg_replace:
$str = "<span class=\"cz\">Dám si jedno pivo prosím.</span> = I'll have a beer please.";
preg_match_all('/[a-ztúuýžácdéeínórš]+/i',$str,$matches);
$matches = array_unique($matches[0]);
foreach ($matches as $match) {
if(!empty($words[$match])) {
$str = preg_replace("/(^|[^\w]){1}(".preg_quote($match,"/").")($|[^\w]){1}/i", '\\1<a href="#">\\2</a>\\3', $str);
}
}
echo $str;
What I'd like to do is restrict the linking to only within the span tag.
My brain is all regex-ed out, so any help would be appreciated! Thanks!
Darren.