2

I'm new to PHP. When I try htmlspecialchars() with ENT_HTML5 flag it works, but all non-English words are removed. I tried:

<?php

$sometext = $_GET['query'];
$sometext = htmlspecialchars($sometext, ENT_HTML5, 'UTF-8');
echo $sometext;

?>

For example, I tried with query "Hello world, Привет мир!" (English, Russian). But it returned me "Hello world, !". I don't have access to a php.ini. Maybe there is a problem? How can I solve this problem?

Thank you, @deceze, I found the solution:

<?php

$sometext = $_GET['query'];
$sometext = htmlspecialchars($sometext, ENT_HTML5, 'Windows-1251');
echo $sometext;

?>
4

1 回答 1

4

The problem is that you're telling PHP that your string is UTF-8 encoded, when it apparently is not. So PHP cannot interpret it correctly and drops all letters which are not UTF-8 encoded.

Actually save your source code file in UTF-8 and/or change the encoding declaration of wherever that string comes from or change the 3rd parameter to the encoding the string is actually saved in.

See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App if you need more information.

于 2013-07-25T11:05:31.727 回答