How to Check If String is URL or Not in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hey Developer,

Now, let's see post of how to check string is url or not in php. This post will give you a simple example of how to check if string is url in php. if you want to see an example of check if string is url php then you are in the right place. you can understand a concept of check value is url or not.

In PHP, you can check if a string is a valid URL or not by using the filter_var() function with the FILTER_VALIDATE_URL filter. Here's an example code snippet:

Example:

<?php

$url = "https://www.example.com";

if (filter_var($url, FILTER_VALIDATE_URL) !== false) {

echo "$url is a valid URL";

} else {

echo "$url is not a valid URL";

}

In this example, the filter_var() function checks if the $url variable is a valid URL using the FILTER_VALIDATE_URL filter. If the URL is valid, it will return the URL string, otherwise it will return false.

You can also use regular expressions to check if a string matches the format of a URL, but using the filter_var() function is a more reliable and convenient method.

I hope it can help you...

Tags :
Shares