IT & Programming

Validate a url with PHP

  • Validate a url with php.
  • How to validate a url with php.
  • Validate a url with php.

Function accepts a parameter and returns false if value is not a valid url.

Function

function isValidUrl($string){

    if(!preg_match('/^(http|https|ftp)://([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?/?/i', $string)){
	return false;
    }
}

Calling The Function

$string='http://www.example.com';

if(isValidUrl($string)!==false){

    echo 'Value is a valid url';

}else{
     
    echo 'Value is not a valid url';
}

Output

Value is a valid url

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *