jhbalaji Posted July 6, 2010 Share Posted July 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/ Share on other sites More sharing options...
jhbalaji Posted July 7, 2010 Author Share Posted July 7, 2010 Any Replies please Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1082243 Share on other sites More sharing options...
salathe Posted July 7, 2010 Share Posted July 7, 2010 Your regular expression should be more specific about what (where) it will match so that if the URL appears within an anchor tag, the match is not successful. This sub-forum has lots of threads trying to do precisely that. Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1082308 Share on other sites More sharing options...
jhbalaji Posted July 7, 2010 Author Share Posted July 7, 2010 In simple words It should replace the keyword i specified into links but whenever it founds HTML Tags say Anchor Tag or Image Tag it should skip the Word in between those tags can also be defined as i need a regex pattern in which it should turn the keywords into links in a Paragraph without messing inbetween HTML Tgas Thanks for your Help salathe Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1082404 Share on other sites More sharing options...
salathe Posted July 8, 2010 Share Posted July 8, 2010 Have you had a look around the forums here? There are loads of threads asking about ignoring stuff within a tag. Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1082913 Share on other sites More sharing options...
sasa Posted July 8, 2010 Share Posted July 8, 2010 try $body = preg_replace('~(^| )'.$textlinksname.'\b~i', $replace,$body,200); Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1082921 Share on other sites More sharing options...
jhbalaji Posted July 8, 2010 Author Share Posted July 8, 2010 Tried that! Some what works Basically if a Image URL has a form of Space the it also replaces that! But it temporarily fixes my problem Thanks Again! Quote Link to comment https://forums.phpfreaks.com/topic/206857-help-me-regarding-preg_replace/#findComment-1083085 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.