【问题标题】:Replace special characters UTF-8 PHP DOM替换特殊字符 UTF-8 PHP DOM
【发布时间】:2013-01-24 21:29:36
【问题描述】:

我有这个代码:

        $strhtml = file_get_contents('05001400300320100033100.html');
        // create the DOMDocument object, and load HTML from a string
        $dochtml = new DOMDocument();
        $dochtml->loadHTML($strhtml);
        $elm = $dochtml->getElementById('upPanelActuciones');
        $segatiel= $dochtml->saveXml($elm);


        $order   = array("á","é","í","ó","ú","ñ");                      
        $replace = array("&aacute","&eacute","&iacute","&oacute","&uacute","&ntilde");
        $megin = str_replace($order, $replace,$segatiel); 

        echo $megin;

但显然 str_replace 函数不起作用,因为输出保留了稀有字符(如 ó)。有没有办法让 str_replace 工作?

提前感谢您的帮助。

pd:我设置了 html charset Utf-8。

【问题讨论】:

    标签: php dom replace


    【解决方案1】:

    更新

    试试这个

    $strhtml = file_get_contents('05001400300320100033100.html');
    $dochtml = new DOMDocument();
    $dochtml->loadHTML($strhtml);
    $elm = $dochtml->getElementById('upPanelActuciones');
    $segatiel= $dochtml->saveXml($elm);
    $trans = get_html_translation_table(HTML_ENTITIES);
    unset($trans["\""], $trans["<"], $trans[">"]);
    $megin = strtr($segatiel, $trans);
    echo $megin;
    

    str_replace 不适用于国际字符。

    <?php
    /**
     * Replace all occurrences of the search string with the replacement string.
     *
     * @author Sean Murphy <sean@iamseanmurphy.com>
     * @copyright Copyright 2012 Sean Murphy. All rights reserved.
     * @license http://creativecommons.org/publicdomain/zero/1.0/
     * @link http://php.net/manual/function.str-replace.php
     *
     * @param mixed $search
     * @param mixed $replace
     * @param mixed $subject
     * @param int $count
     * @return mixed
     */
    if (!function_exists('mb_str_replace')) {
        function mb_str_replace($search, $replace, $subject, &$count = 0) {
            if (!is_array($subject)) {
                // Normalize $search and $replace so they are both arrays of the same length
                $searches = is_array($search) ? array_values($search) : array($search);
                $replacements = is_array($replace) ? array_values($replace) : array($replace);
                $replacements = array_pad($replacements, count($searches), '');
    
                foreach ($searches as $key => $search) {
                    $parts = mb_split(preg_quote($search), $subject);
                    $count += count($parts) - 1;
                    $subject = implode($replacements[$key], $parts);
                }
            } else {
                // Call mb_str_replace for each subject in array, recursively
                foreach ($subject as $key => $value) {
                    $subject[$key] = mb_str_replace($search, $replace, $value, $count);
                }
            }
    
            return $subject;
        }
    }
    ?>
    

    但是你要找的不是 htmlentities() 吗? http://www.php.net/manual/en/function.htmlentities.php

    【讨论】:

    • 好吧,我尝试使用 htmlentities() 但返回带有 html 代码的纯文本。但是使用 saveXml 我可以显示 htlm 结构。所以。有了这个功能(mb_str_replace)我可以做我想做的事吗?
    • mb_str_replace 的工作方式与 str_replace 完全相同,但可以接受国际字符。
    • 我尝试使用你的功能,但我无法让它工作。我不知道我的错误是什么。我将变量重命名为 $search、$replace、$subject,我认为 saveXml 不是数组,所以这个函数不起作用。
    • mb_str_replace($order, $replace, $segatiel);应该适用于您的代码。
    • 不起作用。我真的不知道为什么不能在 saveXml 上工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    相关资源
    最近更新 更多