Test if string is URL encoded in PHP

Posted by Psytronic on Stack Overflow See other posts from Stack Overflow or by Psytronic
Published on 2009-10-28T14:50:20Z Indexed on 2010/06/17 8:33 UTC
Read the original article Hit count: 281

Filed under:
|
|

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)

© Stack Overflow or respective owner

Related posts about php

Related posts about testing