Test if string is URL encoded in PHP

前端 未结 13 2245
不思量自难忘°
不思量自难忘° 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-01 11:39

    I am using the following test to see if strings have been urlencoded:

    if(urlencode($str) != str_replace(['%','+'], ['%25','%2B'], $str))
    

    If a string has already been urlencoded, the only characters that will changed by double encoding are % (which starts all encoded character strings) and + (which replaces spaces.) Change them back and you should have the original string.

    Let me know if this works for you.

提交回复
热议问题