I'm trying to write a PHP function to validate a URL, which based on user input may or may not already be URL encoded.
I know from this answer that spaces should be encoded as such:
You should have
%20
before the?
and+
after.
The core failure in my function is the use of this:
!filter_var($url, FILTER_VALIDATE_URL) === false
Although this will return true with +
in either the path or query string, I have no problem handling it in the path. I can easily split the path from the query string and return false if +
is found in the path (requiring the user to decide on %20
or %2B
).
But my question is what to do if I find +
in the query string? How do I know if this is a proper use of an encoded space, or if it needs to be encoded as %2B
?