PHP - automatic detection of (raw) urlencode

The PHP file receives a URL encoded string through GET. However, some scripts can send strings encoded using a function urlencode(), while other scripts can send strings encoded using a function rawurlencode().

What would be the best way to check which function was used to encode the string, so you can call the corresponding decoding function ( urldecode()or rawurldecode())?

So far, my only idea is this code:

if (stristr($string, "%20"))...
+4
source share
1 answer

, [^0-9A-Za-z_~-], , . : rawurlencode() %20 +, urlencode().

, , %[0-9A-F]{2}, . +, rawurldecode(). , urldecode() - .

<?php
$str = "foo bar baz";
$raw = rawurlencode($str);
$enc = urlencode($str);

echo rawurldecode($raw);
echo rawurldecode($enc);
echo urldecode($raw);
echo urldecode($enc);
?>

:

foo bar baz
foo+bar+baz
foo bar baz
foo bar baz
+2

All Articles