开发者

Non-English characters in PHP

im having a problem with writing a Non-English characters into file (.txt) using php . this is my code :

$str = "â€êþÿûîœøîô‘ë’ðüïlæß€¿×÷¡ï";
$str = htmlentities($str, ENT_QUOTES, mb_detect_encoding($str));
$str =htmlspecialchars_decode(html_entity_decode($str),ENT_QUOTES);
$f = fopen("t开发者_JAVA技巧est.txt","w");
fputs($f,$str);
fclose($f);

when i open the file the result is : â€êþÿûîœøîô‘ë’ðüïlæß€¿×÷¡ï

as you see for example the euro symbol still no appear correctly in the file and other symbols .

any one have an idea to fix this problem ?


The conversion of to € is done by the htmlentities() function; since you are encoding into HTML entities and decoding right after, I'd suggest to leave this step out:

$str = "â€êþÿûîœøîô‘ë’ðüïlæß€¿×÷¡ï";
$f = fopen("test.txt","w");
fputs($f,$str);
fclose($f);


Assuming you want to keep this encoding/decoding business (it looks like you're trying to use the encode/decode process to convert between character sets?):

In your encoding step, you use mb_detect_encoding on the input string and pass that to htmlentities, which allows the euro sign in your input to be correctly detected (most of the time).

However, in your decoding step, you don't specify any charset, so html_entity_decode will pick ISO-8859-1, which doesn't include the euro sign.

If you want to keep this code block mostly the same, you need to pick a charset to decode to that includes all the characters you want (like UTF-8 or ISO-8859-15).

Edit: Here's an example based on your code (I picked ISO-8859-15, but you really need to know or decide what output character set you want):

$str = "â€êþÿûîœøîô‘ë’ðüïlæß€¿×÷¡ï";
$str = htmlentities($str, ENT_QUOTES, mb_detect_encoding($str));
$str = html_entity_decode($str, ENT_QUOTES, 'ISO-8859-15');
$f = fopen("test.txt","w");
fputs($f,$str);
fclose($f);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜