Find a URL in a string...

Discussion in 'PHP' started by iamben, Feb 1, 2009.

  1. #1
    Hey guys -

    Does anyone have a function (or know where I can find one) to find URLs posted in strings? If I had a comment submitted as a string, for instance, I'm trying to prevent it being displayed (or edit it) if it contains a URL / email / web address etc etc.

    I suppose it'd be some sort of reg ex, I just really, really don't want to sit down and figure it out...

    Much appreciated if you can help...


    ben
     
    iamben, Feb 1, 2009 IP
  2. slpshtmike

    slpshtmike Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $raw= "Some text, http://digitalpoint.com";
    $filtered = preg_replace("/(http:\/\/[^\s]+)/", "<a href=\"$1\">$1</a>", $raw);
    echo $filtered;
    PHP:
    This will replace http://digitalpoint.com with <a href="http://digitalpoint.com">http://digitalpoint.com</a>


    $raw= "Some text, http://digitalpoint.com";
    $filtered = preg_replace("/(http:\/\/[^\s]+)/", "", $raw);
    echo $filtered;
    PHP:
    And this will remove the link completely.
     
    slpshtmike, Feb 1, 2009 IP