The result of the string is different from the load in domdocument

I want to get the same result after loading in domdocument. how to do it?

echo "Café"; $s = <<<HTML <html> <head> </head> <body> Café </body> </html> HTML; $d = new domdocument; $d->loadHTML($s); echo $d->textContent; 

first echo result = Café
second echo result = CafÃ

+4
source share
3 answers

You must mark your HTML as UTF-8 encoded

 $s = <<<HTML <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> Café </body> </html> HTML; $d = new domdocument; $d->loadHTML($s); echo $d->textContent; 
+2
source

Your problem is coding,
for the first echo you echo the text using the default encoding,
but for text stolen through a DOMDocument,
e + apostrophe is divided into two characters,

I do not know how to ensure the correct DOMDoc encoding ...
but i'm sure this is your problem

hope i helped, good luck.

+1
source

With the first echo before the HTML, you send HEADERS with your standard server encoding. This ignores any subsequent encodings. You must first respond

 <Html tag and encodings etc.. 

and than echo any other values.

+1
source

Source: https://habr.com/ru/post/1440945/


All Articles