how to check for a url inside a string?

Discussion in 'PHP' started by falcondriver, Jun 17, 2006.

  1. #1
    hi,

    has anyone a nice function to check if there is a valid url inside a string, probably some regular expressions (really not my strong side) or do you think a
    stristr($form, 'http://')
    PHP:
    is enought?

    some idiot tries to spam my side, but im happy to see his domain shut down by his provider after not even 6 hours :D
     
    falcondriver, Jun 17, 2006 IP
  2. DXL

    DXL Peon

    Messages:
    380
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    /:/ should return positive on any string containing an URL, but would also get a lot of false positives.
    If you want to check for Hypertext URLs your code would be sufficient.
     
    DXL, Jun 18, 2006 IP
  3. yugu

    yugu Peon

    Messages:
    98
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try it with preg_match:
    <?php
    // Host name
    preg_match("/^(http:\/\/)?([^\/]+)/i", $form, $matches);
    $host = $matches[2];
    
    // domain
    preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
    $domain = $matches[0];
    ?> 
    PHP:
    ;)
     
    yugu, Jun 20, 2006 IP