php-script 输出 xml 文件,但在特殊字符处中断

标签 php xml

我创建了这个 php 脚本,用于从我的数据库创建 xml 文件:

<?php
header("Content-type: text/xml");
header("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$host = "localhost";
$user = "root";
$pass = "root";
$database = "flexapp";

$charToReplace = array("é", "è", "ê");
$charReplacements = array("e", "e", "e");

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "SELECT * FROM artists";
$artist_result = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$xml_output .= "<artists>\n";

for($x = 0 ; $x < mysql_num_rows($artist_result) ; $x++){
    $itemrow = mysql_fetch_assoc($artist_result);
    $xml_output .= "\t<artist>\n";
    $xml_output .= "\t\t<id>" . $itemrow['pk_artist_id'] . "</id>\n";
    $itemrow['artist_name'] = str_replace($charToReplace, $charReplacements,         $itemrow['artist_name']);
    $xml_output .= "\t\t<name>" . $itemrow['artist_name'] . "</name>\n";
    $xml_output .= "\t\t<picture>" . $itemrow['artist_pic'] . "</picture>\n";
    $xml_output .= "\t\t<twitter>" . $itemrow['artist_twitter'] . "</twitter>\n";
    $xml_output .= "\t</artist>\n";
}
$xml_output .= "</artists>\n";
echo $xml_output;
?>

我尝试将 é en ê 等字符替换为 e。

但是它在浏览器中给出了这个错误:

This page contains the following errors:

error on line 23 at column 9: Encoding error

这是输出的 xml:

<?xml version="1.0" encoding="UTF-8"?>
<artists>
<artist>
    <id>1155</id>
    <name>Have Heart</name>
    <picture>http://userserve-ak.last.fm/serve/126/29086375.jpg</picture>
    <twitter></twitter>
</artist>
<artist>
    <id>1156</id>
    <name>Dead Swans</name>
    <picture>http://userserve-ak.last.fm/serve/126/4781939.jpg</picture>
    <twitter></twitter>
</artist>
<artist>
    <id>1157</id>
    <name>Nirvana</name>
    <picture>http://userserve-ak.last.fm/serve/126/3991355.jpg</picture>
    <twitter></twitter>
</artist>
<artist>
    <id>1158</id>
    <name>Touchter>
</artist>

但是最后一个(数据库中的名称是 Touché Amoré)应该和其他所有一样,但不知何故字符串没有被替换。

最佳答案

这可能是打印前对数据进行编码的问题。尝试使用 utf8_encode()htmlentities()

function xmlencode($data) {
    $data = utf8_encode($data);
    $data = htmlentities($data);
    return $data;
}

然后

...
$xml_output .= "\t\t<picture>" . xmlencode($itemrow['artist_pic']) . "</picture>\n";
...

关于php-script 输出 xml 文件,但在特殊字符处中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8447875/

相关文章:

javascript - 在JQuery中使用CORS获取RSS文件

c++ vtk xml writer (vtkImageData) - 同一个vti文件中的多个变量

php - Laravel Eloquent 多表

java - SHA-1 需要多长时间来创建哈希?

php - MySQL:从 INSERT SELECT 获取返回结果

javascript - 如何修复 "page trying to load scripts from unauthenticated source"

xml - XPATH表达式选择有两个有 child 的 parent

php - SimpleXML 能否只加载 XML 的一部分?

php - Doctrine2实体自定义加入

html - 查找 Xpath - 用于没有 HTML 标记的文本