php - Character Encoding/decoding becomes a mess -
in webapp place <div id="xxx" contenteditable=true >
editing purpose. encodeuricomponent(xxx.innerhtml)
send via ajax post
type server, php script creates simple txt file in turn can downloaded user store locally or print on screen. works perfect far, … yes, but, character encoding mess. special characters german Ä
interpretated wrong. in case ä
google days , study php methods iconv()
, know how set browsers character encoding , set text editor correct correspondending decoding. nothing helps, still messs, or becoming weired.
so question : in encoding/decoding roundtrip browser server , browser have what, ensure Ä
still Ä
?
i answer question, because turns out problem stated above. contenteditable
part of section of html code. on serverside php need filter out contenteditable
text via domdocument this:
$doc = new domdocument(); $doc->loadhtml($_post["data"]);
then access elements , textual content usual. save text
file_put_contents($txtfile, $plaintext, lock_ex);
the saved text mess written above. turns out need tell domdocument character set wich loadhtml()
has interpretate. in case utf-8. first did recommended in php way :
$doc = new domdocument('1.0', 'utf-8');
but doesn't (i wonder). found this answer in so. , final solution :
$doc->loadhtml('<?xml encoding="utf-8">' . $_post["data"]);
though works trick. question left over, how right way ? if somebedoy has definite answer, welcome.
Comments
Post a Comment