Stuyvenstein
Stuyvenstein

Reputation: 2447

PHP SimpleXMLElement special character encoding / decoding

I've got the folowing xml string in php:

$str="<name>An☢maly</name>";

I then do the following:

$tmp=new SimpleXMLElement($str);

if I do:

echo $tmp->name;

I get this:

An☢maly

When I do:

echo utf8_decode($tmp->name);

I get:

An?maly

How do I get PHP to display that funny character, it also does it with the following:

ƎnƔy™ = ?n?y?

Upvotes: 0

Views: 831

Answers (2)

Nostalgie
Nostalgie

Reputation: 564

Try use

header('Content-type:text/html;charset=utf-8');

before output (if you use browser)

P.S: An☢maly - this is output when browser render page in "ISO-8859-1" charset instead "UTF-8"

Upvotes: 1

user1578653
user1578653

Reputation: 5028

That's strange, I'm doing the exact same thing and it is displaying properly in web browser:

<?php
$str="<xml><name>ƎnƔy™</name></xml>";
$tmp=new SimpleXMLElement($str);
echo $tmp->name;
?>

I noticed that it didn't work properly unless I saved the source file as a UTF8 encoded file. I'm using Programmer's Notepad and clicked file > properties and changed the 'Encoding' parameter to UTF-8 to achieve this.

Also, UTF8 characters will not print correctly on windows command line, which may also be your problem.

Upvotes: 1

Related Questions