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
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Question Regular expression help

    I'm trying to create a regular expression that validates a URL.

    For example, a valid URL in this case would match:

    http(s)://www.yahoo.com/file.php?name=some-thing+some_thing+some~thing&page=1#anchor

    So basically the forward slash after the TLD can contain: a-zA-Z0-9#?&=+-:~/._

    This is what I have so far:

    Code:
    $urlpattern = '/^(http(s?):\/\/)((\w+\.)+)\w{2,}(\/?)$/i';
    Thanks!
    Last edited by four0four; 02-10-2010 at 12:24 AM.

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Why not just parse the URL with parse_url() and examine the values returned? Let PHP do the heavy lifting for you.
    http://www.php.net/parse_url

    Or... filter_var():
    http://www.php.net/filter_var
    PHP Code:
    if ( FALSE === filter_var$urlFILTER_VALIDATE_URLFILTER_FLAG_SCHEME_REQUIRED ) )
    {
        
    // URL is invalid

    Untested, but should work.
    Last edited by kbluhm; 02-10-2010 at 12:57 AM.

  • Users who have thanked kbluhm for this post:

    four0four (02-15-2010)

  • #3
    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;
      } 
    I wrote this ages ago. If you preg_match(NET_URL_REGEX, $url) that should be a very reliable check for URL validity.
    Last edited by Lamped; 02-10-2010 at 01:02 PM.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • Users who have thanked Lamped for this post:

    four0four (02-15-2010)


  •  

    Posting Permissions

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