How to validate URLs by a small RegEx Function:
function valid_url($str) {
return (!preg_match(‘/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i’, $str)) ? FALSE : TRUE;
}
And how does it work:
^ is an anchor meaning it must start with the pattern
(http|https|ftp) matches and captures one of the three
:\/\/ is :// literally. (\/ is / escaped)
([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+)
(letter meaning a-z in the following)
1 letter or number
any number of letters, numbers, _ or -.
A literal dot
1 letter or number
any number of letters, numbers, _ or -.
+ means the previous pattern 1 or more times, and the grouping is around (?:\.[A-Z0-9][A-Z0-9_-]*)+, so it means that pattern any number of times. (It allows for .co.uk for example. )
(\d+)?\/?/i
0-1 :
Any amount of digits
An optional literal /
You may also like this Tutorial for regular Expressions:
>>Regular Expression Tutorial

Loading ...