I got a keyword var from an url like this:
search.php?keyword=%E5%AE%89%E5%85%A8
with utf8
encoding. Then I want to convert the keyword to this format:
安全
So I can use it in MySQL/PHP/
How can I achieve this?
I got a keyword var from an url like this:
search.php?keyword=%E5%AE%89%E5%85%A8
with utf8
encoding. Then I want to convert the keyword to this format:
安全
So I can use it in MySQL/PHP/
How can I achieve this?
This should work.
$html = mb_convert_encoding($_GET['keyword'], 'HTML-ENTITIES', 'UTF-8');
echo htmlspecialchars($html);
//安全
First one is url encoded, and what you want are html entities.
$string = urldecode('%E5%AE%89%E5%85%A8'); // == 安全
$string2 = htmlentities($string); // == 安全
$string3 = htmlspecialchars($string2); // == 安全
The one from your question is double encoded (&
got converted to &
) which I assume is wrong.
23433
is decimal and equals hexadecimal x5B89
. Same for the second code. For the browser, it doesn't matter if it's decimal or hexadecimal.
If you really intend double encoding, use htmlspecialchars($string);
on the above code.