Re: preg_replace question

From: Date: Mon, 24 Jan 2011 17:15:48 +0000
Subject: Re: preg_replace question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-311006@lists.php.net to get a copy of this message
On 1/24/2011 8:00 AM, Merlin Morgenstern wrote: > 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 > Do you have control over the building of the initial "phrase" => "link" assoc? If so, reverse the order of these two items. Jim Lucas

« previous php.general (#311006) next »