I'm aware of filter_var()
and it's FILTER_VALIDATE_URL
filter. The point is there are some URLs which are exist but not count as a valid URL and I need to verify them. For example, URLs with spaces.
At the moment I am checking only those protocols that application is interested in (http, https and ftp) using strpos()
.
But I was wondering if there is a more generic method in PHP that I could employ?
It might help if i explain that i need to differentiate if the target source is a URL or a local path.
Use function parse_url()
to split the URL into components then do some basic analysis on the pieces it returns (or just check if the returned value is an array()
or FALSE
).
As the documentation says:
This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted,
parse_url()
tries its best to parse them correctly.
And also:
On seriously malformed URLs,
parse_url()
may returnFALSE
.
It looks like it matches your request pretty well.