1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problem with replacing Using PHP

Discussion in 'PHP' started by roopajyothi, May 31, 2010.

  1. #1
    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
    SEMrush
    So is there any way to skip the text containing http:// using preg replace function ?

    Thanks
     
    roopajyothi, May 31, 2010 IP
    99d7737 likes this.
    SEMrush
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    $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.
     
    danx10, May 31, 2010 IP
  3. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #3

    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
     
    roopajyothi, May 31, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Your confusing me, ellaborate, show me the input and expected output.
     
    danx10, May 31, 2010 IP
  5. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #5
    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!
     
    roopajyothi, May 31, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    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:
     
    danx10, May 31, 2010 IP
  7. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #7
    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
     
    Last edited: May 31, 2010
    roopajyothi, May 31, 2010 IP
  8. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #8
    <?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:
     
    danx10, Jun 1, 2010 IP
  9. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #9
    Thanks a lot
    Rep added
    You saved me!
     
    roopajyothi, Jun 1, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    
    <?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 :)
     
    roopajyothi, Jul 6, 2010 IP