如何测试字符串是否经过 URL 编码?
以下哪种方法更好?
- 在字符串中搜索将被编码的字符,哪些不是,如果存在则它没有被编码,或者
- 使用我制作的类似这样的东西:
function is_urlEncoded($string){
$test_string = $string;
while(urldecode($test_string) != $test_string){
$test_string = urldecode($test_string);
}
return (urlencode($test_string) == $string)?True:False;
}
$t = "Hello World > how are you?";
if(is_urlEncoded($sreq)){
print "Was Encoded.\n";
}else{
print "Not Encoded.\n";
print "Should be ".urlencode($sreq)."\n";
}
上面的代码有效,但在字符串被双重编码的情况下无效,如以下示例所示:
$t = "Hello%2BWorld%2B%253E%2Bhow%2Bare%2Byou%253F";
$t = "Hello+World%2B%253E%2Bhow%2Bare%2Byou%253F";