Hot to replace words with preg_replace without duplicates

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi there,

I want to create a function which will replace certain words out of a text with internal links. That works so far, but if I have two matches, I end up with invalid html code.

Example:
Welpen 			/hunde
Chihuahua Welpen 	/hunde,chihuahua

function seo_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);
}

seo_internal_links($ad[text], $seo_internal_links, $limit = 1);

This will result in:
<a href="//hunde,chihuahua">Chihuahua <a href="/hunde">Welpen</a></a>

Has somebody an idea on how to avoid this? I would also like to limit the amount of hits, but the limit in preg_replace accounts only for unique words, not the whole array.

Thank you for any hint,

Merlin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux