Special characters from xml file don't display correctly using php

夙愿已清 提交于 2019-12-11 06:42:19

问题


This may be a stupid question but its not a matter of what I can find, its a matter that I dont know what to search for. There are some special characters that don't show correctly in php. I'm taking some information from an xml file, and echo-ing them.

ie:

should be -> Nürnberg

echoes as -> Nürnberg

any tips on what to look for, or how to resolve this?


回答1:


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"  />



回答2:


You simply have an encoding mismatch. Get up to speed with these articles:

What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text
Handling Unicode Front To Back In A Web App




回答3:


try a different character set on the page you're echoing from

http://www.w3schools.com/tags/ref_charactersets.asp




回答4:


Can you try with following meta tag in your HTML head.

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"  />



回答5:


There is a mismatch between the character encoding of your XML and what you are outputting from PHP. Most likely, one is UTF-8 and one is ISO-8859.

On the PHP side, you can set this with a header directive

<?php
header('Content-Type: text/plain; charset=ISO-8859-1');
header('Content-Type: text/plain; charset=utf-8');
?>

and/or in the outputted HTML

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

On the XML side, most quality text editors allow you to specify the character encoding as you save the file. (E.g. WordWrangler on Mac)

If the XML file is indeed in ISO-8859, you could use utf8_encode() to convert it to UTF-8 as you read it in.

An in-depth discussion of PHP and character encoding.




回答6:


"I'm taking some information from an xml file, and echo-ing them."

Windows command line doesn't support utf8 properly as it doesn't use an UTF8 font.

Just put the file into somewhere that's reachable through a web server and test it by calling the file through the web server. Alternatively pipe the output of the script into a text file:

php test.php > output.txt

And either open output.txt is a UTF8 capable editor or use a utf8 capable 'Tail' program.

Test.php

<?php
    echo "Nürnberg";
?>

Running from command prompt:

php test.php

Nürnberg

Calling through a web server http://localhost/test.php

Nürnberg


来源:https://stackoverflow.com/questions/9123370/special-characters-from-xml-file-dont-display-correctly-using-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!