PHP validation/regex for URL


I've been looking for a simple regex for URLs, does anybody have one handy that works well? I didn't find one with the zend framework validation classes and have seen several implementations.

5/5/2019 9:04:10 PM

Accepted Answer

I used this on a few projects, I don't believe I've run into issues, but I'm sure it's not exhaustive:

$text = preg_replace(
  "'<a href=\"$1\" target=\"_blank\">$3</a>$4'",

Most of the random junk at the end is to deal with situations like in a sentence (to avoid matching the trailing period). I'm sure it could be cleaned up but since it worked. I've more or less just copied it over from project to project.

2/5/2015 11:15:43 AM

Use the filter_var() function to validate whether a string is URL or not:

var_dump(filter_var('', FILTER_VALIDATE_URL));

It is bad practice to use regular expressions when not necessary.

EDIT: Be careful, this solution is not unicode-safe and not XSS-safe. If you need a complex validation, maybe it's better to look somewhere else.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow