Re: preg_replace question
| From: | David Harkness | Date: | Mon, 24 Jan 2011 17:05:48 +0000 |
| Subject: | Re: preg_replace question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-311004@lists.php.net to get a copy of this message | ||
Without seeing the code that creates the arrays, it's tough to see the
problem. It looks like the first replacement is catching "Beagle Welpen"
entirely since the closing </a> tag gets placed after "Welpen". Then the
second replacement does just "Welpen".
Also, you should have quotes around "link" when building the $replace[]
entry since the array access is outside quotes. Finally, you don't need $k
here at all.
// create internal links
function internal_links($str, $links, $limit) {
foreach($links AS $link){
$pattern[] = "~\b($link[phrase])\b~i";
$replace[] = '<a
href="'.$link['link'].'">\\1</a>';
}
return preg_replace($pattern,$replace,$str, $limit);
}
David