Recently i was Writing a Preg replace Function in PHP Here is the Code below $suchmuster = "/".$textlinksname."/i"; $replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>"; $body = preg_replace($suchmuster,$replace,$body,200); PHP: Since the variable $body contains links begins with http:// when it replaces the matched text in URL it becomes a problem Similarly for the images too So is there any way to skip the text containing http:// using preg replace function ? Thanks
$replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>"; $body = preg_replace('~'.preg_quote($textlinksname).'~i',$replace,$body,200); PHP: Use preg_quote(), and you don't neccesarily need to use the / (slash) as the modifier, I always prefer to use ~. As the / is common amongst urls and choosing a different modifier may prevent the need to escape.
oh no my friend here is a example No when doing replacements with preg_replace is there was anyway that it won't do any replacements if it found a URL Thanks Dan
Oh Dan! Its Simple $textlinksname = 'Google'; $textlinksurl = 'http://google.com'; $body = 'Google is a great search engine. Here is the logo of it <img src =\'http://google.com/logo.jpg\'>'; $suchmuster = "/".$textlinksname."/i"; $replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>"; $body = preg_replace($suchmuster,$replace,$body,200); // Here the Output will be // <a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it <img src ='http://<a href='http://google.com' target='_blank'>Google</a>.com/logo.jpg'> // It replaces the Word 'Google' even in the URL(Image) as result the links and image links get broken // So, I expect the output must be like this //<a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it <img src ='http://google.com/logo.jpg'> // Such that It replaces only text and skip the text in the URL's PHP: Thanks Dan!
I don't work here, nor should you rely on me as this is completely voluntary, furthermore its 2.19am (UK Time) here theirfore a different timezone to you. <?php $textlinksname = 'Google'; $textlinksurl = 'http://google.com'; $body = 'Google is a great search engine. Here is the logo of it <img src =\'http://google.com/logo.jpg\'>'; $replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>"; $body = preg_replace('~(?<!http://)'.preg_quote($textlinksname).'~i', $replace,$body,200); echo $body; /* output: <a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it <img src ='http://google.com/logo.jpg'> */ ?> PHP:
Dan i tried but run and see this and Further updated the code a little bit to work with WWW or Without WWW but when the text appears in the URL(Say Directory Structure It messes) See Below <?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\'/>'; $body = str_ireplace('http://www.','http://',$body); $body = str_ireplace('https://www.','http://',$body); $replace = "<a href='".$textlinksurl."' target='_blank'>".$textlinksname."</a>"; $body = preg_replace('~(?<!http://)'.preg_quote($textlinksname).'~i', $replace,$body,200); echo $body; // Output will be // <a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it <img src ='http://google.co.in/<a href='http://google.com' target='_blank'>Google</a>/images/srpr/logo1w.png'/> //But i need output like this //<a href='http://google.com' target='_blank'>Google</a> is a great search engine. Here is the logo of it <img src ='http://google.co.in/google/images/srpr/logo1w.png'/> // Such that it wont replace any text occurring in the URL ?> PHP: Any Help? Thanks Dan
<?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; ?> PHP:
<?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; ?> PHP: Oops still there is a problem Last time i tested wrongly! Still the problem is there 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'/> Code (markup): 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'/> Code (markup): I mean it also replaces the word inside the HTML tags Can anyone help me on how to prevent that??? Thanks in advance