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.

Need help with preg_replace

Discussion in 'PHP' started by deriklogov, Jan 12, 2009.

  1. #1
    Hi ,


    Need somebody help
    I have content and in that content there are some links to another website. I need to remove those links , but not all of them only to specific website
    I think the best is to use preg_replace , but not sure how the pattern would looks exactly. I would really appreciate your help.Thank you very much.
     
    deriklogov, Jan 12, 2009 IP
  2. deriklogov

    deriklogov Well-Known Member

    Messages:
    1,078
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    130
    #2
    More detailed Example:

    Text:
    Without much effort, you can be <a href='yahoo.com'>Yahoo is the easy hosting</a> a master of disguise today. You don't even need to put on a costume or mask. However you see yourself, others will see you that way too. You appear busier than <a href='google.com'>Google is big engine</a>you actually are now because it helps you to avoid taking on additional work. Instead of putting up a smokescreen of illusion, just set your
    boundaries by telling others what you can and can't do.


    Pattern:
    I need to remove all links contains word Google in url


    Replace: replace whole link with white space
    <a href='google.com'>Google is big engine</a> , whole link should be removed not just google
     
    deriklogov, Jan 12, 2009 IP
  3. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    linkexchange1984, Jan 13, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    You missed the point completely...

    <?php
    
    $text = "Without much effort, you can be <a href='yahoo.com'>Yahoo is the easy hosting</a> a master of disguise today. You don't even need to put on a costume or mask. However you see yourself, others will see you that <a href='http://somesite.lol.google.com/page.html'>way</a> too.  <a href='www.google.com'>Google</a> is big engine. You appear busier than <a href='google.com'>Google is big engine</a> you actually <a href='http://google.com'>Google</a> is big engine are now because it helps you <a href='http://web.google.com'>Google</a> to avoid taking on additional work. Instead of putting up a smokescreen of illusion, just set your boundaries by telling others what you can and can't do.";
    
    /** Matches (examples)
    
     * http://google.com
     * google.com
     * www.google.com
     * http://web.google.com
     * http://somesite.lol.google.com/page.html
     * (Basically any link with google in it)
    
     */
    
    preg_match_all( '/(?P<find><a href=\'(?:http:\/\/|ftp:\/\/|https:\/\/)?(?:www\.)?(?:(?:[a-z0-9-_\.])*?)?google(?:[^\']*?)\'>(?P<replace>[^<]*?)<\/a>)/i', $text, $matches, PREG_SET_ORDER );
    
    $find = array() and $replace = array();
    
    foreach ( $matches as $match )
        $find[] = $match['find'] and $replace[] = $match['replace'];
    
    echo str_replace( $find, $replace, $text );
    
    PHP:
    If you need any alterations please reply back.
     
    Danltn, Jan 13, 2009 IP