使用php htmlentities将引号转换为HTML输出的html实体

时间:2014-04-01 18:39:30

标签: php

创建一些XML时遇到问题。 我正在查询MySQL数据库以创建XML。

正常创建XML。 当我在“摘要”字段中添加单个引号的文本时,我收到编译错误 - 没有详细信息。

我已经在代码中添加了htmlentities()函数来解决这个问题,但它没有帮助。

当我删除带有单引号的字段内容时,XML渲染得很好,所以我知道问题出在该字段的内容中。 [额外评论:如果我只是从文本中单引号中正确编译]

以下是我正在使用的代码。我们将非常感激地收到任何提示。 “summary”字段是导致错误的字段。

    <?php  

require("../xxxxxxx.php"); 

// Start XML file, create parent node

$dom = new DOMDocument("1.0");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node); 

// Opens a connection to a MySQL server

$connection=mysql_connect ($viewer_host, $viewer_user, $viewer_pass);
if (!$connection) {  die('Not connected : ' . mysql_error());} 

// Set the active MySQL database

$db_selected = mysql_select_db($viewer_db, $connection);
if (!$db_selected) {
  die ('Can\'t use db: ');//. mysql_error()
} 

// Select all the rows in the markers table

$query = "SELECT * FROM markers WHERE 1";
$result = mysql_query($query);
if (!$result) {  
  die('Invalid query: ' . mysql_error());
} 

header("Content-type: text/xml"); 

// Iterate through the rows, adding XML nodes for each

while ($row = @mysql_fetch_assoc($result)){  
  // ADD TO XML DOCUMENT NODE  
  $node = $dom->createElement("marker");  
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("id",$row['id']); 
  $newnode->setAttribute("name",htmlentities($row['name']));
  $newnode->setAttribute("summary", htmlentities($row['summary']));
  $newnode->setAttribute("address", htmlentities($row['address']));
  $newnode->setAttribute("region", htmlentities($row['region'])); 
  $newnode->setAttribute("lat", $row['lat']);  
  $newnode->setAttribute("lng", $row['lng']);  
  $newnode->setAttribute("type", $row['type']);
  $newnode->setAttribute("artform", htmlentities($row['artform']));
} 

echo $dom->saveXML();

?>

0 个答案:

没有答案