Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder Ludatha's Avatar
    Join Date
    Jan 2008
    Posts
    250
    Thanks
    51
    Thanked 5 Times in 5 Posts

    Validating a URL

    Hey guys, I am trying to get a perfect regex that validates a url, but I cannot seem to find one.

    The best one I have is this:
    PHP Code:
    preg_match("/^(http(s?):\\/\\/|ftp:\\/\\/{1})((\w+\.)+)\w{2,}(\/?)$/i"$_POST['url']) 

    But it doesnt validate a url like this:
    Code:
    http://forums.digitalpoint.com/showthread.php?t=1215701#post10533297
    You cannot have anything after the .com

    I need it to support directories:
    Code:
    http://www.ludatha.com/user/
    Files:
    Code:
    http://www.ludatha.com/user.php
    _GET Variables
    Code:
    http://www.ludatha.com/user.php?id=Adam
    Anchors
    Code:
    http://www.ludatha.com/user.php?id=Adam#comments

    Can anyone find me or code me one that works ?

    Thanks

  • #2
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    PHP Code:
    define('NET_URL_REGEX',
      
    '@
      (?# Protocol)
        ([a-z]{1,6}://)
      (?# Login)
        (?:([^\:]*)(?:\:([^\@]*))?\@)?
      (?# Domain)
        ([a-z0-9][a-z\.\-]*[a-z])
      (?# Port)
        (\:[0-9]{1,5})?
      (?# Path)
        (/[a-zA-Z0-9\$\-\_\.\+\!\'\(\|\)\,\;\/\@\&\=\%]* )?
      (?# Query)
        (\?[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\|\)\,\;\/\?\:\@\&\=\%]*)?
      @Sx'
    );

    function 
    net_split_url($url) {
        
    /* Near perfect uri matching and splitting */
      
    if (preg_match(NET_URL_REGEX,$url,$matches)) {
          
    $details = array(
            
    'protocol' => $matches[1],
            
    'username' => $matches[2],
            
    'password' => $matches[3],
            
    'domain' => $matches[4],
            
    'path' => $matches[5],
            
    'query' => $matches[6]
          );
          if (
    $details['query']) {
            
    $query explode('&',$details['query']);
            
    $details['query'] = array();
            foreach(
    $query as $item) {
                list(
    $name,$value) = explode('=',$item,2);
                
    $details['query'][urldecode($name)] = urldecode($value);
            }
          }
          return 
    $details;
      } else {
          return 
    False;
      }

    Some code I wrote once. Seems pretty accurate. Comes with a free url splitting function to demonstrate.

  • #3
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Oh, I missed the anchor. Well feel free to add (?:#([a-zA-Z0-9]*))? under match[7] if you like.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •