Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php regular expression for external links

Options
  • 20-06-2007 12:20pm
    #1
    Registered Users Posts: 2,031 ✭✭✭


    I'm trying to do a regular expression to indentify external links and add an icon to them.

    So far I've got:

    [PHP]
    $pattern = '/<a href="?(.*:\/\/)?([^ \/]*)([^ >"]*)"?[^>]>(.*)(<\/a>)/';
    $replacement = '<a href="$1$2$3">$4 <img src="/images/icon_web.gif" alt="external link" /></a>';
    $content = preg_replace($pattern, $replacement, $content, -1);
    print $content;
    [/PHP]

    But it only seems to put the icon on the last link on the page.

    Any help much appreciated.


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt


    from the top of my head your match should have a ig or something added to it ... case insensitive and multiple times ...

    the $pattern should possible have ig at the end
    $pattern = '/<a href="?(.*:\/\/)?([^ \/]*)([^ >"]*)"?[^>]>(.*)(<\/a>)/ig'; 
    


    edit: ah the -1 takes care of that .. sorry

    Edit Edit: just ran your code and its working fine so I'm confused :)


  • Registered Users Posts: 2,031 ✭✭✭colm_c


    It doesn't seem to work with more than 1 link in the text.

    Did you try it with a couple of links?


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    colm_c wrote:
    It doesn't seem to work with more than 1 link in the text.

    Did you try it with a couple of links?


    Code sample I ran ...
    <?PHP
    
    $content = <<<END
    		<ul>
    				<li><a href="http://us.ard.yahoo.com/SIG=12lgn363s/M=385895.10615157.11509033.7674020/D=yahoo_top/S=2716149:FPAD/_ylt=AjIVmKHx06EPO2i9ahEcDdf1cSkA/Y=YAHOO/EXP=1182346617/A=4545677/R=8/SIG=14ufch571/*http://shopping.yahoo.com/b:Diamond%20Buying%20Guide:784708673;_ylc=X3oDMTFsY2kzZGxyBF9TAzI3MTYxNDkEc2VjA2ZwLWhtYW50bGUEc2xrA2ZpbGxlcmFkLWFwcmlsLWRpYW1vbmRz">Diamond</a></li>
    				<li><a href="http://us.ard.yahoo.com/SIG=12lgn363s/M=385895.10615157.11509033.7674020/D=yahoo_top/S=2716149:FPAD/_ylt=AjIVmKHx06EPO2i9ahEcDdf1cSkA/Y=YAHOO/EXP=1182346617/A=4545677/R=9/SIG=14ukb9290/*http://shopping.yahoo.com/b:Jewelry%20Buying%20Guide:784708697;_ylc=X3oDMTFrOGdrMjlpBF9TAzI3MTYxNDkEc2VjA2ZwLWhtYW50bGUEc2xrA2ZpbGxlcmFkLWFwcmlsLWpld2Vscnk-">Jewelry</a></li>
    				<li><a href="http://us.ard.yahoo.com/SIG=12lgn363s/M=385895.10615157.11509033.7674020/D=yahoo_top/S=2716149:FPAD/_ylt=AjIVmKHx06EPO2i9ahEcDdf1cSkA/Y=YAHOO/EXP=1182346617/A=4545677/R=10/SIG=15544heiq/*http://shopping.yahoo.com/b:Engagement%20Buying%20Guide:784708681;_ylc=X3oDMTFua2tyYW82BF9TAzI3MTYxNDkEc2VjA2ZwLWhtYW50bGUEc2xrA2ZpbGxlcmFkLWFwcmlsLWVuZ2FnZW1lbnQ-">Engagement</a></li>
    			</ul>
    END;
    
    $pattern = '/<a href="?(.*:\/\/)?([^ \/]*)([^ >"]*)"?[^>]>(.*)(<\/a>)/';
    $replacement = '<a href="$1$2$3">$4 <img src="/images/icon_web.gif" alt="external link" /></a>';
    $content = preg_replace($pattern, $replacement, $content, -1);
    print $content;  
    
    ?>
    


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    sorry but why are you not using css to do this?

    http://www.askthecssguy.com/2006/12/showing_hyperlink_cues_with_cs_1.html


  • Registered Users Posts: 2,031 ✭✭✭colm_c


    @forbairt: That didn't seem to work for me, I had to do an explode in the end and preg_match each of the items in the array.

    @Ph3n0m: That CSS doesn't work in IE6, so it's really any good to me. I could also have just added a class to each external link, but this will work in IE6 but if the links wrap the icon doesn't go to the end of the link it goes to the end of the left most part of the link.

    Like this:

    This is text and this is <x>
    a link


    The icon would be where the <x> is


  • Advertisement
  • Registered Users Posts: 3,594 ✭✭✭forbairt


    a[href $='.pdf'] { 
       padding-right: 18px;
       background: transparent url(icon_pdf.gif) no-repeat center right;
    }
    

    you learn something new everyday ... always knew the [ type = ] for input boxes but didn't realise you could match to the end of a href :)


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    but as pointed out - it doesnt work on IE 6


Advertisement