Smarty can encode URLs directly, such as <!--{$var |urlencode}-->
But in the smarttemplate inside as if not, because the link is submitted by JS, rather than form submission, so can not automatically encode.
Solution: Use JS to the URL of the Chinese characters to escape code.
<a href= "onclick=" window.open (' product_list.php?p_sort= ' +escape (' PHP Development Resource Network '); " >
So click on the effect of the link after:
Reference: http://127.0.0.1/shop/product_list.php?p_sort=PHP%u5F00%u53D1%u8D44%u6E90%u7F51
It is obvious that using PHP's UrlDecode () or Base64_decode () is not solvable.
Workaround, write an inverse function in PHP:
Copy Code code as follows:
function Js_unescape ($STR)
{
$ret = ';
$len = strlen ($STR);
for ($i = 0; $i < $len; $i + +)
{
if ($str [$i] = = '% ' && $str [$i +1] = = ' U ')
{
$val = Hexdec (substr ($str, $i +2, 4));
if ($val < 0x7f) $ret. = Chr ($val);
else if ($val < 0x800) $ret. = Chr (0xc0| ( $val >>6)). Chr (0x80| ( $val &0x3f));
else $ret. = Chr (0xe0| ( $val >>12)). Chr (0x80| ( ($val >>6) &0x3f)). Chr (0x80| ( $val &0x3f));
$i + 5;
}
else if ($str [$i] = = '% ')
{
$ret. = UrlDecode (substr ($str, $i, 3));
$i + 2;
}
else $ret. = $str [$i];
}
return $ret;
}
Note that the JS code will be automatically converted into UTF-8, so must be coded conversion to get the correct results, otherwise it will be Chinese garbled.
The code is as follows:
Print iconv (' utf-8 ', ' gb2312 ', Js_unescape ($_request[' p_sort '));
Here we have successfully reversed the JS escape code.
As follows:
References: PHP Development Resource Network
In addition, I found a php to implement JS escape encoding function:
Copy Code code as follows:
function Phpescape ($STR)
{
$sublen =strlen ($STR);
$retrunString = "";
for ($i =0; $i < $sublen; $i + +)
{
if (Ord ($str [$i]) >=127)
{
$tmpString =bin2hex (Iconv ("gb2312", "Ucs-2", substr ($str, $i, 2));
$tmpString =substr ($tmpString, 2,2). substr ($tmpString, 0,2); You might want to open this item under window
$retrunString. = "%u". $tmpString;
$i + +;
} else {
$retrunString. = "%" Dechex (ord ($str [$i]);
}
}
return $retrunString;
}
Have you ever encountered such a problem?