[SOLVED] Regex giving me problems

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

[SOLVED] Regex giving me problems

Post by HiddenS3crets »

I use this function to validate a URL

Code: Select all

function validateURL($url) {
        if(preg_match("/^http:\/\/www\.+[A-Za-z0-9]+\.a-z{2,4}/", $url))
                return true;
        else
                return false;
}
The problem is that it always returns false, no matter what I pass in. It should return true if $url is something like "http://www.somesite.com"

There must be a problem with the regex, but i'm not sure what it is (still in the process of learning regex).

Any ideas where the bug is?
Last edited by HiddenS3crets on Tue Sep 05, 2006 8:33 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hint: you're missing something around the latter "a-z"
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post by HiddenS3crets »

is it [ and ]? I tried doing that and it still returned false :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

All I can guess is you put them in the wrong spot(s).
Post Reply