0

Hello everyone i am writing a snippet for replacing the keywords into links.
It almost works but it also replaces the words in between HTML Tags
So can anyone help me in fixing that

<?php
$textlinksname = 'Google';
$textlinksurl = 'http://google.com';
$body = 'Google is a great search engine. Here is the logo of it <img src =\'http://www.google.co.in/google/images/srpr/logo1w.png\'/>';

$replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>";
$body = preg_replace('~(?<!http://|www\.|/)'.$textlinksname.'~i', $replace,$body,200);

echo $body;
?>

It outputs as

<a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it
 <img src ='http://www.google.co.in/google/intl/en_com/images/srpr/google-123-<a href='http://google.com' target='_blank'>Google</a>.png'/>

But i need output like this such that it wont mess with HTML Tags

<a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it
 <img src ='http://www.google.co.in/google/intl/en_com/images/srpr/google-123-Google.png'/>

Can anyone help me on how to prevent that???

Thanks in advance :)

Edited by jhbalaji: n/a

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by pritaeas
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.