preg_match function and regular expression to check url is valid or not if it is valid it will show message url is valid other message will be url is invalid



#example.php

<?php
$url = "https://rathorji.in/p/how_to_validate_url_in_php";
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $url)) {
    echo $url. " is Invalid URL";
}else{
    echo $url. " is valid URL";
}
?>

Run the following code and see the result


Output:

https://rathorji.in/p/how_to_validate_url_in_php is valid URL