Page 1 of 1

Another url check thread

Posted: Tue Sep 21, 2010 9:32 am
by JKM
Hi there,

yes - another url check thread, but I couldn't find any thread for exactly my question.

if $url starts with 'http://' or 'www.', $urlCheck should return as true, else as false;

Code: Select all

$url = 'http://test.com';    
if(preg_match('/(http:\/\/|www.)/i', $url)) {
    $urlCheck = true;
} else {
    $urlCheck = false;
}
Thanks.

Re: Another url check thread

Posted: Tue Sep 21, 2010 10:55 am
by AbraCadaver
Works for me, though you need to escape the . in your pattern.

Re: Another url check thread

Posted: Tue Sep 21, 2010 1:10 pm
by McInfo
That pattern does not necessarily match the beginning of the string. See Anchors.

Also, you can simplify the code by replacing the if and else blocks with type casting.

Code: Select all

$result = (bool) preg_match($pattern, $subject);

Re: Another url check thread

Posted: Tue Nov 02, 2010 11:49 am
by JKM
thanks guys, but if I want preg_match_all to check for urls and return the url, how should I do it then? If I use '/(http:\/\/|www\.)/i', it only returns http://.