DEV Community

Techsolutionstuff
Techsolutionstuff

Posted on • Originally published at techsolutionstuff.com

How To Validate URL In PHP With Regex

In this article, we will see how to validate URLs in PHP with regular expressions. Also, you can implement it in laravel or PHP. here we will give you an example of validating URL with a regular expression and without regular expression. So, you can both way to implement validate URL in PHP or laravel.

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. So, here we will provide the simple PHP code to validate URL in PHP.

So, let's see validate URL, PHP gets URL, URL validation regex laravel, valid URL using a regular expression, how to validate URL in laravel, URL validation regex PHP.

PHP provide filter_var() inbuilt function with FILTER_VALIDATE_URL filter. So, we can easily validate a URL in PHP using javascript regex or regular expression.

Example : PHP FILTER_VALIDATE_URL Filter

The FILTER_VALIDATE_URL filter validates a URL.

<!DOCTYPE html>
<html>
<body>

<?php
// Variable to check
$url = "https://www.techsolutionstuff.com";

// Validate url
if (filter_var($url, FILTER_VALIDATE_URL)) {
  echo("Enter URL is a valid URL");
} else {
  echo("Enter URL is not a valid URL");
}
?>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Possible flags of validating URL in PHP.

FILTER_FLAG_HOST_REQUIRED - URL must include hostname (like https://www.techsolutionstuff.com)
FILTER_FLAG_PATH_REQUIRED - URL must have a path after the domain name (like www.techsolutionstuff.com/post)
Enter fullscreen mode Exit fullscreen mode

Read Also : How to Remove Elements From Array In Javascript


Example : Validate URL With Regex

Above function is simply to check URL validation. If you want to check or validate URLs manually or more securely then you can use regular expression. So, here I will give an example of code.

<?php 

$regex = "((https?|ftp)\:\/\/)?";
$regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?";
$regex .= "([a-z0-9-.]*)\.([a-z]{2,3})";
$regex .= "(\:[0-9]{2,5})?";
$regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?";
$regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?";
$regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?";

$url = 'https://techsolutionstuff.com/';

if (preg_match("/^$regex$/i", $url)) {
   echo('Enter URL is a valid URL');
}
Enter fullscreen mode Exit fullscreen mode

You might also like :

Read Also : How To Generate QRcode In Laravel

Read Also : Google Map With Draggable Marker Example

Read Also : How To Create Dependent Dropdown In Laravel

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair • Edited

There are many more common schemes than http, https and ftp, though - and anyone can register their own on your machine if they want.

This will validate using your regex: https://foo.bar:99999 even though it uses an invalid port, while mailto schemes will only work some of the time (depending on where you use colons and so on, and the need to validate an email address being super complicated). The fact that mailto will work at all is a side-effect of your regex thinking it's a username/password section rather than a scheme, and I expect there are combinations which will fail.

I agree that this will get most cases you're likely to care about, and it's probably better than whatever PHP does with FILTER_VALIDATE_URL, but it's never going to be exhaustive unless you write a URL parser.