One solution is to use filter_var for your initial validation and then perform additional validation checks if the original filter_var validation passes.
Validating URLs is important to form handling and PHP data processing.
I’ve just come across a pretty common requirement to convert any text that looks like a link into a link within some HTML text.
Strangely, after searching for a good 15 minutes for a regular expression, all I could find was either a regular expressions which detects URLs with a protocol such as or a regular expression which detects URLs without such as
[A-Za-z0-9\.\-] // anything looking at all like a domain ) ( // brackets covering match for path, query string and anchor (?
If you're going to allow dotted IPs you should really allow 32-bit IPs too, e.g., and
It uses a fairly ugly-looking regular expression.
Because of this one may be tempted to simply copy the best regex you can find (gist).
For checking the format of the url it seems to me to be the most logical approach to use regular expressions.
to find a good regular expression to that validates urls not url domains. I don’t really need to validate ftp so I should have removed the ftp protocol from the list of choices.
One that doesn’t allow spaces in the domainname and where the domain can be suffixed with the port number. I need this just to validate urls for weblinks and the link element in an rss feed.
Just because browsers tend to understand them as a matter of a historical accident does not mean those are valid URIs, just as tag soup that browsers also tend to understand isn't valid HTML either. It's much simpler to write a URL validator by hand, speaking as someone who wrote a URL parser, and fixed a bug in PHP's.Or, you know, use a robust existing validator or parser.
Like PHP's, for instance. https://github.com/Taze TSchnitzel/Faucet-HTTP-Extension - granted, this deliberately limits the space of URLs it can parse, but it's not difficult to cover all valid cases if you need to https://github.com/php/php-src/commit/36b88d77f2a9d0ac74692a... It's much simpler to write a URL validator by hand... I am firming up a feature right now that detects when a user types a url into a text field and replaces it on the fly with a footnote-style reference number (much like your comment above).