Forum Moderators: coopster
for example texti love google result
the output printed will be like this
[HTML]i love <a href="http://google.com">google</a> result[/HTML]
it is just replacing google text into a link like this <a href="http://google.com">google</a>
but the problem is if the original google text is already inside a link, like this
[HTML]<a href="http://google.com">i love google result</a>[/HTML]
the output printed will be broken like this, there will be double hyperlink
[HTML]<a href="http://google.com">i love <a href="http://google.com">google</a> result</a>[/HTML]
<?php
$string = '
<p>but the problem is if the original google text is already inside a link, like this
<a href="http://google.com">i love google result</a></p>
<p>I love yahoo</p>
<p>No Google is better</p>
<p>Bingo, boingo, bongo.</p>
<p>Doesn\'t anyone remember Lycos?</p>
';
echo "<p>Before</p> $string";
$links = Array(
'Google' => 'http://google.com',
'Yahoo' => 'http://yahoo.com',
'Bing' => 'http://www.bing.com',
'Lycos' => 'http://www.lycos.com'
);
foreach ($links as $key => $value) {
$strip = "<a[^>]+>.*" . $key . "[^<]*<\/a>";
$string = preg_replace("/$strip/ism",$key,$string);
$string = preg_replace("/$key/im","<a href=\"$value\">$key</a>",$string);
}
echo "<p>After</p> $string";
?>