简体   繁体   中英

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?

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.

Try use

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

before output (if you use browser)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM