How can I test if a string is URL encoded?
Which of the following approaches is better?
In my case I wanted to check if a complete URL is encoded, so I already knew that the URL must contain the string https://
, and what I did was to check if the string had the encoded version of https://
in it (https%3A%2F%2F
) and if it didn't, then I knew it was not encoded:
//make sure $completeUrl is encoded
if (strpos($completeUrl, urlencode('https://')) === false) {
// not encoded, need to encode it
$completeUrl = urlencode($completeUrl);
}
in theory this solution can be used with any string other than complete URLs, as long as you know part of the string (https://
in this example) will always exists in what you are trying to check.