1

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?

simbabque
  • 53,749
  • 8
  • 73
  • 136

2 Answers2

1

This should work.

$html = mb_convert_encoding($_GET['keyword'], 'HTML-ENTITIES', 'UTF-8');
echo htmlspecialchars($html);
//安全
Phil
  • 1,996
  • 1
  • 19
  • 26
0

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.

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • 2
    There is no need to urldecode GET arguments. PHP does this automatically. http://php.net/manual/en/function.urldecode.php#refsect1-function.urldecode-notes – Phil Jun 22 '15 at 19:51