Test if string is URL encoded in PHP
- by Psytronic
Hey guys, I've looked through the PHP Docs and can't see anything to do with this, so how can I test if a string is URL encoded? Is it better to search the string for characters which would be encoded, which aren't, and if any exist then its not encoded, or use something like this which I've made
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";
}
Which works, however not in instances where this might occur
$t = "Hello%2BWorld%2B%253E%2Bhow%2Bare%2Byou%253F";
I.e. where the string has been doubly encoded, or maybe this string
$t = "Hello+World%2B%253E%2Bhow%2Bare%2Byou%253F";
I.e. where most has been doubly encoded, except for one space. (Yes I don't know when this string would ever occur, but you never know)