Test if string is URL encoded in PHP

前端 未结 13 2199
不思量自难忘°
不思量自难忘° 2021-01-01 11:10

How can I test if a string is URL encoded?

Which of the following approaches is better?

  • Search the string for characters which would be encoded, which
13条回答
  •  耶瑟儿~
    2021-01-01 11:41

    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.

提交回复
热议问题