【问题标题】:PHP simplexml EntitiesPHP simplexml 实体
【发布时间】:2011-02-20 11:56:08
【问题描述】:

这是怎么回事?

$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
    <album>
        <img src="002.jpg" caption="w&aacute;ssup?" />
    </album>
XML;

$xml = simplexml_load_string($string);
// $xmlobj = simplexml_load_file("xml.xml"); // same thing

echo "<pre>";
var_dump($xml);
echo "</pre>";

错误:

警告:simplexml_load_string() [function.simplexml-load-string]:实体:第 5 行:解析器错误:未定义实体“aacute”

【问题讨论】:

    标签: simplexml php entities


    【解决方案1】:

    试试这个函数simplexml_load_entity_string

    <?php
    
    $string = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
        <album>
            <img src="002.jpg" caption="test&lt;w&aacute;ssup?" />
        </album>
    XML;
    
    $xml = simplexml_load_entity_string($string);
    
    var_dump($xml);
    
    function simplexml_load_entity_string($string = '')
    {
        // cover entity except Predefined entities in XML
        $string = str_replace([
            '&quot;', '&amp;', '&apos;', '&lt;', '&gt;',
        ], [
            'SPECIALquotMARK', 'SPECIALampMARK', 'SPECIALaposMARK', 'SPECIALltMARK', 'SPECIALgtMARK',
        ], $string);
        $string = html_entity_decode($string, ENT_QUOTES, "utf-8");
        $string = str_replace([
            'SPECIALquotMARK', 'SPECIALampMARK', 'SPECIALaposMARK', 'SPECIALltMARK', 'SPECIALgtMARK',
        ], [
            '&quot;', '&amp;', '&apos;', '&lt;', '&gt;',
        ], $string);
    
        // load xml
        return simplexml_load_string($string);
    }
    

    【讨论】:

      【解决方案2】:

      另一种解决方法是改变

      "w&aacute;ssup?" to "w&amp;aacute;ssup?"

      【讨论】:

        【解决方案3】:

        您可能希望查看Matt Robinson's article 的另一种方法:在 PHP 中将命名实体转换为数字。它提到了html_entity_decode 方法(已经被另一个答案指出)和一些潜在的陷阱:

        这种方法有两个可能的问题。第一个是无效实体:html_entity_decode() 不会触及它们,这意味着您仍然会收到 XML 错误。二是编码。我想您可能实际上并不想要UTF-8。你应该,因为它很棒,但也许你有充分的理由。如果您不告诉html_entity_decode() 使用UTF-8,它将不会转换您指定的字符集中不存在的实体。如果您告诉它以 UTF-8 输出,然后使用 iconv() 之类的东西进行转换,那么您将丢失任何不在输出编码中的字符。

        另外,如果你觉得脚本比较麻烦,也可以使用shared on SourceRally

        【讨论】:

        • 好的,但是如何获取数据呢?使用 file_get_contents()?
        • $feed = file_get_contents('xml.xml'); // 对字符串做任何你需要的事情,这样解析器就不会在这里出错... $xmlObj = simplexml_load_string( $feed );
        【解决方案4】:

        前几天我遇到了这个问题。 任何出现的 & 都需要在 CDATA 标记内

        <album>
            <img src="002.jpg" />
            <caption><![CDATA[now you can put whatever characters you need & include html]]></caption>
        </album> 
        

        防止解析器失败。

        【讨论】:

        • 如果img 标记应该保留为 HTML,那就太好了。取决于 OP 想要什么,要么是这个,要么是解码实体。
        • 是的,但我不能使用 CDATA,XML 文件需要像这样。带有属性中的标题。 Pekka,我怎样才能解码实体?我应该使用 file_get_contents() 获取 XML 字符串而不是解码吗?
        【解决方案5】:

        &amp;aacute 不是XML entity - 您正在考虑 HTML。

        特殊字符通常在 XML 中“按原样”使用 - 输入数据上的 html_entity_decode()(不要忘记指定 UTF-8 作为字符集)应该可以解决问题:

        $string = html_entity_decode($string, ENT_QUOTES, "utf-8");
        

        【讨论】:

        • Pekka,在我的示例中,如果我这样做:$xmlStr = file_get_contents("xml.xml"); $xml = html_entity_decode($xmlStr, ENT_QUOTES);我得到 caption="w�ssup?"
        • @FFish 你添加了utf-8吗?
        • 不,我没有!几分钟内输入太多。现在可以了:-)
        • 我在读取带有 encoding="iso-8859-1" 的 XML 文件并插入带有 utf-8 的数据库时遇到问题(行中的字段在第一个重音字符处被截断,而print_r 是完美的)。将“utf-8”添加到 html_entity_decode 已解决。谢谢。
        • 但标题添加&amp;lt;,仍然无法解析。见sandbox.onlinephpfunctions.com/code/…
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-06
        • 2012-06-22
        相关资源
        最近更新 更多