Jump to content

[SOLVED] characters screwed up with file_get_contents


frankiej

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.