1

好的,我有一个名为“ temp.xml ”的文件,其中包含大约 100 个条目。这是子条目的示例..

<title>&#169; 2008 Some Company</title>

现在,我想获得“标题”中的确切内容,但通过这样做......

$doc = new DOMDocument();
$doc->load('temp.xml');
foreach ($doc->getElementsByTagName('entry') as $node) {
echo $node->getElementsByTagName('title')->item(0)->nodeValue;
}

我得到...

© 2008 Some Company

而且我要...

&#169; 2008 Some Company

Php 正在转换 ASCII 字符,我希望它们完好无损。我现在在谷歌上搜索了两个多小时无济于事......

4

1 回答 1

0

好的,似乎使用 XAMPP 与 MySql 的连接不是在 UTF-8 中完成的......所以这就是解决我的问题的方法......

$link2 = mysql_connect('localhost', 'root', '');
mysql_set_charset('utf8',$link2); //Set the connection to UTF-8
mysql_select_db('mydb') or die( "Unable to select database");
$doc = new DOMDocument();
$doc->load('temp.xml');
foreach ($doc->getElementsByTagName('entry') as $node) {
$title = $node->getElementsByTagName('title')->item(0)->nodeValue;
$query = "INSERT INTO sb_temp (id, title) VALUES ('', '$title')";
$result = mysql_query($query);
}

我只是将mysql连接设置为... mysql_set_charset('utf8',$link2); 现在数据按原样保存。否则,例如“©”符号被保存为“©”。无论如何感谢您的帮助:]

于 2012-09-20T14:30:16.747 回答