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("test.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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.