How to Validate URL in PHP

When a URL is submitted from a form input by the user, it is very important to check this URL is valid or not before taking any action on it. Here we’ll provide the simple PHP code to validate a URL in PHP.

Code

$website = $_POST["website"];
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
  $message = "Invalid URL";
}

Leave a Reply

Your email address will not be published. Required fields are marked *