Add links to specific words within span tag in PHP

Posted by dazhall on Stack Overflow See other posts from Stack Overflow or by dazhall
Published on 2011-01-11T14:42:30Z Indexed on 2011/01/11 14:54 UTC
Read the original article Hit count: 263

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about regex