Add to Favourites Add to Favourites    Print this Article Print this Article

I need a good php URL decoder for API testing

This is the php code that I use to test all new API output:



url decode


$str_s='';
if (isset($_POST['str']))
       $str_s=$_POST['str'];

$sub_array = true;
if ($str_s!='' && !(isset($_POST['sub_array']) && $_POST['sub_array'] == 'yes'))
       $sub_array = false;
?>




> Contains multi-level URL encoding








show_enc($str_s);

function show_enc($str2, $tab=0)
{
       global $sub_array;

       $c = "style='margin-left: ".(($tab+1)*20)."px'";
       echo "
";

       parse_str($str2, $output);

       foreach ($output as $key => $value)
       {
               echo "
".html_char(urldecode($key))." = ";

               if ($sub_array && strpos($value, '=') !== false)
                       show_enc($value, $tab+1);
               else
                       echo html_char(urldecode($value));

               echo "
";
       }
       echo "
\n";
}

function html_char($str)
{
       $h = htmlspecialchars(stripslashes($str), ENT_QUOTES | ENT_HTML401);
       if ($h == '' && $str != '')
               return htmlspecialchars(utf8_encode(stripslashes($str)), ENT_QUOTES | ENT_HTML401);
       else
               return $h;
}


?>


If you need to decode a list similar to:

list[]=value1&list[]=value2&list[]=value3&list[]=value4

then use something similar to this:

$str="list[]=value1&list[]=value2&list[]=value3&list[]=value4";
$a = explode('&', $str);
$values = Array();

$i=0;
foreach ($a as $v)
{
   $values[$i++] = substr(strstr($v, '='), 1);
}

print_r($values);


Was this answer helpful?

Also Read