Jump to content

simplexml and non-standard characters


maddogandnoriko

Recommended Posts

I have an xml file loaded into a simplexml object. The xml has a "ö" in it and is getting garbled in the simplexml object. Here is my test code:

 

<?php 
      $seriesURL='http://www.thetvdb.com/api/GetSeries.php?seriesname=krod';
      echo 'SeriesURL:'.$seriesURL.'<br><br>';
      $data = simplexml_load_file($seriesURL);
      
      echo '<br><br>'. ($data->Series->SeriesName);
?>

 

The resulting Series Name is: Kröd Mändoon and the Flaming Sword of Fire

 

Link to comment
https://forums.phpfreaks.com/topic/154112-simplexml-and-non-standard-characters/
Share on other sites

http://www.thetvdb.com/api/GetSeries.php?seriesname=krod seem a valid utf-8 xml file.

 

Try that at the begining of your php :

 

<?php
header('Content-Type: text/html; charset=utf-8');
...
?>

 

And in your output html :

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>

...

</body>
</html>

 

It tell the browser (or whatever client) that your output is encoded in utf-8. Because the content in the xml is encoded in utf-8 (like it should).

 

Or you can convert your data into ASCII html entities and it will work no matter what encoding you use with htmlentities() like that :

 

<?php
...
$output = htmlentities($data->Series->SeriesName, ENT_QUOTES, "UTF-8");
echo $output;
...
?>

Don't encode the whole XML, only the data part.

 

http://www.php.net/manual/en/function.htmlentities.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.