测试字符串是否为 PHP 格式的 URL 编码
2022-08-30 16:17:33
如何测试字符串是否经过 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";