tags:

views:

51

answers:

3
//URL START
$urlregex = "^(https?|ftp)\:\/\/";
// USER AND PASS
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
// HOSTNAME OR IP
$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*";
// PORT
$urlregex .= "(\:[0-9]{2,5})?";
// PATH
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
// GET Query
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?";
// ANCHOR
$urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
// check
if (eregi($urlregex, $url)) {echo "good";} else {echo "bad";}

but what if I have http://www.example.com/about-me/my-4*-hotel/

That eregi check isn't valid because of the asterisk. What should I do?

A: 

You could change your regex to allow stars...

$urlregex .= "(\/([a-z0-9+\$_*-].?)+)*\/?"; 
Jens
dunno why, but it doesn't work, I've tried that earlier
Mihai Iorga
@Mihai: I have an error in my regex, maybe you've made that, too. `-` has a special meaning if its not the first or last character in a class. Correcting.
Jens
+1  A: 

From the documentation:

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Use filter_var with FILTER_VALIDATE_URL instead. Or if you insist on using regular expression then use preg_*.

Mark Byers
thanks a lot! that worked :)
Mihai Iorga
+1  A: 

I think your problem is in the URL, not the regex. Last time I checked, asterisk was not permitted in the path portion of a URL. If you really need an asterisk there you should escape it:

http://www.example.com/about-me/my-4%2A-hotel/
Alan Moore
i know that there is a problem, but the old guy that had the project made some stupid things with the URLs, and until I'll remake the URL-rewriting i have to bare with his code. That asterisk has a big importance in the URL.
Mihai Iorga