0

Hello friends i have a problem with some characters reading a xml file from php i am using this source code:

$file = 'test.xml';
$xml_1 = simplexml_load_file($file);

echo ($xml_1->content);

its work ok but when the content is a special character like ñ ó it show a rarer character like this ñ i tried to include in html head utf8 charset but its the same

htmlpower
  • 169
  • 1
  • 16
  • you can show `test.xml` ? – Naumov Apr 25 '16 at 09:38
  • Almost certainly, text.xml is not actually stored in utf-8, and that's your problem. – Chris Lear Apr 25 '16 at 09:40
  • Yes i can see test.xml perfectly the problem is only when it contains special characters like ñ ó.. you can see instead of them ñ... – htmlpower Apr 25 '16 at 09:43
  • Are you viewing the output as utf8? – Imre L Apr 25 '16 at 09:57
  • yes i have in my head html page, in the same page other parts of the page works with this characters this part is the only that doesnt work in the xml file the information is stored with rarer characters must i to convert before be shown? – htmlpower Apr 25 '16 at 10:07
  • 1
    Adding a `` line is similar to adding a note that says "This book is written in English": it's not very useful is the book is in English, it's rather confusing if the book is in French and it won't translate the book in either cases. – Álvaro González Apr 25 '16 at 10:26

2 Answers2

0

SimpleXML emits UTF-8 output by design. If you application does not support UTF-8 you'll have to convert with the usual tools (e.g. mb_convert_encoding()) but you need to take this into account:

  • You need to know for sure the encoding your app is using.
  • UTF-8 can hold the complete Unicode catalogue thus some characters may not have an equivalent in your target encoding.

Whatever, in 2016 there's no reason to use anything else than UTF-8 unless your maintaining legacy code.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • if i see the xml file i can see that the information is stored in rarer characters when i put ñ for example it is saved in the xml file like "ni ño" the real word is "niño", this is a blog aplication and when i see the post i can see correctly but if i extract the information directly form the xml it doesnt work in the blog page i only can see utf-8 charset in the head an it works i dont understand why dont work – htmlpower Apr 25 '16 at 10:01
  • 1
    Make sure you open the XML file with an editor that supports UTF-8. Even if that's the case, you might need to set the encoding explicitly. – Álvaro González Apr 25 '16 at 10:05
  • should i to convert the data before be shown ? i think that the blog app use an extra process to convert it to correct character when it extract from xml file before be shown i think that it is not enought whith put utf8 charset in the html because i have other parts of the web write directly in the html code with ñ ó and i can see perfectly – htmlpower Apr 25 '16 at 10:12
  • If you've built a blog from scratch and you don't even know what text encoding is, everything is possible. You might even have corrupt information in your database. My advice is that you learn about UTF-8 and try to fix your complete app to use such encoding, one layer at a time. Please check out classic question [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through). – Álvaro González Apr 25 '16 at 10:23
0

Finally i find the solution i must to use utf8_decode php function to convert the characters it is not enought with put utf8 charset in the head page you must to convert using php before

htmlpower
  • 169
  • 1
  • 16
  • This is similar to my answer, except that the details make it wrong. As [the manual](http://php.net/utf8_decode) clearly explains, this function converts **from ISO-8859-1** to UTF-8. Applying it blindly to a string that does not have a known encoding means adding yet another layer of bugs. – Álvaro González Apr 26 '16 at 08:31