preg_replace question

From: Date: Mon, 24 Jan 2011 16:00:50 +0000
Subject: preg_replace question
Groups: php.general 
Request: Send a blank email to php-general+get-311002@lists.php.net to get a copy of this message
Hi there, I am trying to replace certain words inside a text with php. Unfortunatelly my function is creating invalid html as output. For example the words "beagle" and "welpen" have to be replaced inside this text: "süße knuffige Beagle Welpen ab sofort" My result looks like this: zwei süße knuffige <a href="/bsp/hunde,beagle">Beagle <a href="/bsp/hunde">Welpen</a></a> The problem is, that my function is not closing the href tag before it starts to replace the next item. Here is the code: ################################################################################################ // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[$k] = "~\b($link[phrase])\b~i"; $replace[$k] = '<a href="'.$link[link].'">\\1</a>'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } ################################################################################################I I could not find a way to fix this and I would be happy for some help. Thank you in advance! Merlin

« previous php.general (#311002) next »