简体   繁体   English

PHP htmlentities输出不正确

[英]PHP htmlentities output not correct

I am trying to build a website that lists poi locations. 我正在尝试建立一个列出poi位置的网站。

I have been using the google PHP MySQL store locator tutorial but still have a few issues. 我一直在使用Google PHP MySQL商店定位器教程,但仍然有一些问题。

The first problem is converting the string from the MYSQL database to a format that is compatible with XML 第一个问题是将字符串从MYSQL数据库转换为与XML兼容的格式

eg 例如

I have a record where the name of the POI is 'Sérvier' with accent on the é 我有一条记录,其中POI的名称为'Sérvier',并在é上加上了重音

I use this to extract the name: 我用它来提取名称:

$newnode->setAttribute("name", htmlentities($row['Name'], ENT_SUBSTITUTE, "ISO-8859-15"));

But it outputs: 但是它输出:

S& amp;eacute;vrier

(I have had to add spaces between the & and amp to show what I mean but I was expecting (我必须在&和放大器之间添加空格以显示我的意思,但我期望

"S& eacute;vrier"

What am I doing wrong? 我究竟做错了什么?

I believe it is as @MichaelRushton suggested something to do with using DOMDocument 我相信这就像@MichaelRushton建议使用DOMDocument有关

If I simply output the results into an html table format, I get the expected results 如果仅将结果输出为html表格式,则可以得到预期的结果

// Iterate through the rows, adding XML nodes for each
echo "<table>";
while ($row = @mysql_fetch_assoc($result)){
echo "<tr>";
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("id", $row['ID']);
echo "<td>". $row['ID']."</td>";
$newnode->setAttribute("name", htmlentities($row['Name'], ENT_QUOTES, "ISO-8859-15"));
echo "<td>".$row['Name']."</td>";
$newnode->setAttribute("type", $row['Type']);
echo "<td>".$row['Type']."</td>";
$newnode->setAttribute("lat", $row['Latitude']);
echo "<td>".$row['Latitude']."</td>";
$newnode->setAttribute("lng", $row['Longitude']);
echo "<td>".$row['Longitude']."</td>";
$newnode->setAttribute("distance", $row['distance']);
echo "<td>".$row['distance']."</td>";
echo "</tr>";
}
echo "</table>";
echo $dom->saveXML();

Reading the tutorial again it does suggest 再次阅读本教程并建议

Note: If your database contains international characters or you otherwise need to force UTF-8 output, you can use utf8_encode on the outputted data 注意:如果您的数据库包含国际字符,或者您需要强制输出UTF-8,则可以对输出的数据使用utf8_encode

But I have no idea how to force UTF-8 output for the XML 但是我不知道如何为XML强制输出UTF-8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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