frankiej Posted January 27, 2009 Share Posted January 27, 2009 I use file_get_contents to get an xml file that has a heading that looks like this: <?xml version="1.0" encoding="iso-8859-1" ?> It contains swedish characters. Then i use SimpleXML to rip out the info from the xml file. Problem is that when it is outputed...the swedish characters are messed up. I guess it has something to do with file_get_contents from this page: http://se.php.net/manual/en/function.file-get-contents.php How can i fix it so that swedish characters are NOT messed up when displaying the webpage? Please help...kind of urgent. Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/ Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 define "messed up" are you encoding it correctly ? ie UTF-8 ? try adding <?php header('Content-Type: text/html; charset=utf-8'); ?> to the start of the page Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747485 Share on other sites More sharing options...
frankiej Posted January 27, 2009 Author Share Posted January 27, 2009 No. I want my webpage to display iso-8859-1 So my webpage looks like this: <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <meta http-equiv="content-language" content="se"> Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747493 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 No. I want my webpage to display iso-8859-1 Erm okay use <?php header('Content-Type: text/html; charset=iso-8859-1'); ?> Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747592 Share on other sites More sharing options...
frankiej Posted January 27, 2009 Author Share Posted January 27, 2009 header() are used to send headers. And i already did that with my page. My page looks like this(index.php): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>test</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <meta http-equiv="content-language" content="se"> </head> <body> <?php $xmlStr = file_get_contents('sysfile.xml'); $xml = new SimpleXMLElement($xmlStr); $res=$xml->xpath("users"); if (is_array($res)) { ...alot of code } </body> </html> ?> My sysfile.xml looks like this: <?xml version="1.0" encoding="iso-8859-1" ?> <users> <user> <name>Göran Karlsson</name> <position>PR</position> </user> ...next user and so on </users> Both of the file is iso-8859-1 but still. The output will not display swedish characters. But if i change the encoding from my webbroswer (IE6) to UTF-8 the swedish characters are shown. I even tried to change my webpage to: <meta http-equiv="content-type" content="text/html; charset=UTF-8"> ...still does not work. And the encoding of the actual webpage should be iso. So how to fix this? I never experienced any of this problem before. It must have to do something with get_file_contents() that is messed up. Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747749 Share on other sites More sharing options...
haku Posted January 27, 2009 Share Posted January 27, 2009 header() are used to send headers. And i already did that with my page. I don't see where you have sent any headers in your code. One thing to check is that the document itself has been saved in ISO-8859-1. If you save the document in UTF-8, a different charset, it can and will cause you problems. Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747755 Share on other sites More sharing options...
frankiej Posted January 27, 2009 Author Share Posted January 27, 2009 header() are used to send headers. And i already did that with my page. I don't see where you have sent any headers in your code. One thing to check is that the document itself has been saved in ISO-8859-1. If you save the document in UTF-8, a different charset, it can and will cause you problems. <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> is equal to: header('Content-Type: text/html; charset=iso-8859-1'); and i also tried to add it on the top of the page...still not working. And it was saved as "iso-8859-2" (from what i can see) but that should not cause problems. Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747814 Share on other sites More sharing options...
MadTechie Posted January 27, 2009 Share Posted January 27, 2009 I just tried this code <?xml version="1.0" encoding="iso-8859-1" ?> <users> <user> <name>Göran Karlsson</name> <position>PR</position> </user> ...next user and so on </users> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>test</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <meta http-equiv="content-language" content="se"> </head> <body> <?php $xmlStr = file_get_contents('sysfile.xml'); $xml = new SimpleXMLElement($xmlStr); $nodes = $xml->xpath('/users/user/name'); echo $nodes[0]; ?> </body> </html> and yes it failed.. BUT.. change <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> to <meta http-equiv="content-type" content="text/html; charset=UTF-8"> And Guess what.. it works! BUT as you want iso-8859-1 No. I want my webpage to display iso-8859-1 Your going to have to decode it from UTF-8 to iso-8859-1 So Final Script <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>test</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <meta http-equiv="content-language" content="se"> </head> <body> <?php $xmlStr = file_get_contents('sysfile.xml'); $xml = new SimpleXMLElement($xmlStr); $nodes = $xml->xpath('/users/user/name'); echo utf8_decode($nodes[0]); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-747839 Share on other sites More sharing options...
frankiej Posted January 29, 2009 Author Share Posted January 29, 2009 thanks. I solved myself a couple of days ago by decoding it from UTF-8. I read that "file_get_contents" always get´s UTF8 from the file it get´s it contents. Thanx guys for all the answers. Link to comment https://forums.phpfreaks.com/topic/142612-solved-characters-screwed-up-with-file_get_contents/#findComment-749601 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.