Miguel Mas
Miguel Mas

Reputation: 545

Parsing xml with PHP what to do with characters like these

I'm parsing an xml document using php.

When I see the result in my browser I get the following characters:

ñ instead of spanish ñ

í instead of í

á instead of á

ó instead of ó

é instead of é

I was going to use a str_replace and replace every odd character for the good ones, but sadly the pattern before happens only sometimes and in general I have a wide collection of odd characters :(

The xml heading is:

<?xml version="1.0" encoding="iso-8859-1"?>

But if I change it to utf-8 it simply won't be printed ..

I load the xml as a string with simplexml_load_string (comes from database like that)

Can you please give me any ideas on how to solve this?

Thanks a lot

Upvotes: 2

Views: 1293

Answers (4)

davidsonsns
davidsonsns

Reputation: 381

// new xml
$xml = new SimpleXMLElement('new.xml'); 

// Displaying XML in textual form
echo $xml->asXML();

Upvotes: 0

Martin M&#252;ller
Martin M&#252;ller

Reputation: 2535

You have 2 options:

a) include a header('Content-Type: text/html; charset=iso-8859-1'); before any output in your php file.

b) convert the output to utf-8 with $str = mb_convert_encoding($str, 'UTF-8', 'ISO-8859-1');

Both should do the trick.

Upvotes: 2

Johndave Decano
Johndave Decano

Reputation: 2113

$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);

Upvotes: 0

jonas
jonas

Reputation: 125

SimpleXML uses UTF-8 to encode stored strings. You can use an XML-File with iso-8859-1, but if you want to print XML values with this encoding, you have to use utf8_decode before.

Upvotes: 0

Related Questions