SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a regular expression for a web address?

    OK, I hate them .. no, I loath them.

    But, hate or loath them, you need 'em.

    I'm looking for a nice expression to validate a web address.

    The one I have at the minute works for others but simply will not work for me:
    Code:
    $pattern = "/^(http|https|ftp)\://((([a-z_0-9\-]+)+(([\:]?)+([a-z_0-9\-]+))?)(\@+)?)?(((((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))))|((([a-z0-9\-])+\.)+([a-z]{2}\.[a-z]{2}|[a-z]{2,4})))(([\:])(([1-9]{1}[0-9]{1,3})|([1-5]{1}[0-9]{2,4})|(6[0-5]{2}[0-3][0-6])))?(/)?$/";
    So, anyone here know either a fix for this or know of a better one?

    Thanks in advance...

  2. #2
    $this->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Dec 2003
    Location
    Federal Way, Washington (USA)
    Posts
    1,524
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's one:

    Code:
    $pattern = "^(http|https|ftp)://(www\.)?.+\.(com|net|org)$";
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    Showcase your music collection on the Web

  3. #3
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but that relies on a list of TLD's. That's not going to work unless I populate it with all of the TLD's.

    Although not difficult, it's something I want to avoid. I'd prefer the expression to be smart enough to work with whatever TLD is thrown at it...

  4. #4
    SitePoint Addict trogdor1024's Avatar
    Join Date
    Oct 2004
    Location
    New Jersey
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you could verify that the TLD portion of the string is between 2 and 4 alpha characters, that might help a little.

  5. #5

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also want to consider that any of the following URLs ist valid

    http://www.microsoft.com:80/
    http://207.46.244.188:80/
    http://anonymous:password@207.46.244.188:80/

  6. #6
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by trogdor1024
    you could verify that the TLD portion of the string is between 2 and 4 alpha characters, that might help a little.
    Which is what I'm chipping away at right now.

    Problem is, what about the likes of: 'co.uk'? That's where I start to loose the plot a little.

    The expression I posted is a mystery to me. I found it on some .asp forum and gave it a go, to no avail.

    I'll see what I can do.

    Thanks for the feedback, guys! I'll let you know what happen, if not for your sake, but for others who might be searching on just this topic...

  7. #7
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try out this link for URI regexps

    http://regexlib.com/DisplayPatterns....1&categoryId=2

    I find that http://regexlib.com is a great resource for testing regexps and for lots of helpful advice.

  8. #8
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdev
    I find that http://regexlib.com is a great resource for testing regexps and for lots of helpful advice.
    That's where I got my expression from .. the one that doesn't work!

    I'll have a look at the other website...

  9. #9
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There's something else not working, here.

    I've tried the above example and that won't work, either. Here's my method:
    Code:
    	// check whether input is a valid web address
    	function isWebAddress($field, $value, $msg) {
    
    		$value = $this->_getValue($value);
    		$pattern = "^(http|https|ftp)://(www\.)?.+\.(com|net|org)$";
    
    		if(preg_match($pattern, $value)) {
    
    			return true;
    
    		} else {
    
    			$this->validateArrayErrorList[] = array("field" => $field, "msg" => $msg);
    
    			return false;
    
    		} // end if
    
    	} // end function isWebAddress
    It's strange, because I've got another method for email addresses that works fine.

    This method just doesn't want to work!

  10. #10
    SitePoint Wizard swdev's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    1,053
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what does
    PHP Code:
      $value $this->_getValue($value); 
    return?
    Try echoing out the value of $value before and after this call.
    Last edited by swdev; Jan 5, 2005 at 07:32. Reason: typo

  11. #11
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by swdev
    what does
    PHP Code:
      $value $this->_getValue($value); 
    return?
    Try echoing out the value of $value before and after this call.
    Just did that.

    It's a method that calls htmlspecialchars().

    The output of the $value is fine. I'm not sure what's happening!

    Like I said, I've got a similar method for emails and that works fine...

  12. #12
    It's been real... Forbes's Avatar
    Join Date
    Dec 2004
    Location
    Yorkshire, England
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what I've cooked up so far:
    Code:
    $pattern = "/^(http|https|ftp)://(www\.)?.+\.([[:alpha:]]{1,})(\.?[[:alpha:]]{1,})?$/";
    It's probably horribly wrong...


Bookmarks

Posting Permissions

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