HELP! PHP preg_match URL

Discussion in 'PHP' started by highborn, Feb 17, 2008.

  1. #1
    Hi,

    I need help about preg_match URL.

    Here is my problem.

    How can I validate form if a user input this in the form. ex. http://www.domain.com or www.domain.com

    The form should only accept domain.com.

    if the http://www and www. are exists in the user's input the php code should output error like

    "Must not contain http:// and www."

    if the input is valid script continue.

    Hope someone could help me about this.

    Thanks,

    Highborn
     
    highborn, Feb 17, 2008 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Instead of that, why not just strip it out? if you take out the http:// and www., then it can continue even if they do add it

    
    $domain = str_replace(array('http://', 'http://www.'), '', $_POST['domain']);
    
    PHP:
    Not too sure, but it'd remove the http and the http://www, if you only stripped the www. they could have (for example) blawww.com and it'd be removed.
     
    decepti0n, Feb 17, 2008 IP
  3. highborn

    highborn Active Member

    Messages:
    184
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    thanks for the reply but I would like to learn how preg_match work on validation.
     
    highborn, Feb 17, 2008 IP
  4. FabioDeMelo

    FabioDeMelo Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    // get host name from URL
    preg_match('@^(?:http://)?([^/]+)@i',
       "http://www.php.net/index.html", $matches);
    $host = $matches[1];
    
    // get last two segments of host name
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: {$matches[0]}\n";
    ?>
    PHP:
     
    FabioDeMelo, Oct 21, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    if (preg_match('#^http://(?:w{3}\.)?#i', $url)) {
    
    echo 'Must not contain http:// and www.';
    
    } else {
    
    //valid....
    
    }
    PHP:
     
    danx10, Oct 22, 2010 IP