I can’t get this function working correctly:
function isValidURL($url){
return preg_match('%http://domain\.com/([A-Za-z0-9.-_]+)/([A-Za-z0-9.-_]+)%', $url);
}
The url:
http://domain.com/anything-12/anything-12/
can contain numbers, letters and symbols _ –
I assume its to do with the first regex – as these work
http://domain.com/anything12/anything12/
http://domain.com/anything12/anything-12/
http://domain.com/anything12/any-thing-12/
http://domain.com/anything_12/any-thing-12/
As always all help is appreciated and thanks in advance.
You need to escape the
-in the character class of your regex.You need to anchor your regex so that tries to match the entire input string and not part of it.
The modified regex is:
You can shorten your regex by noting that
[A-Za-z0-9_]is same as\wand also there is a repeating sub-regex.