javascript - 何时不应在 HTML 链接中使用 & 实体(和)?

标签 javascript php html dom html-entities

何时不应在 HTML 链接中使用 & 实体 ( &)?

上下文:我问的原因是我正在使用 DOMDocument()转换 <img>标记到不同的 HTML,并且符号被复制。对于我的具体示例,我认为这是由于 mb_convert_encoding() 而发生的,但如果我不使用它,我还有其他问题。也许还有其他时候不应该在 HTML 链接中使用 & 实体?

public static function substituteImg($template, $values, $classI='autoInsert', $classF='',$escape=false) {
    $classesToReplace = array($classI);
    if($template) {
        $doc = new DOMDocument();
        $template = mb_convert_encoding($template, 'HTML-ENTITIES', 'UTF-8');
        $doc->loadHTML($template);

        $xpath = new DOMXPath($doc);
        foreach( $xpath->query( '//img') as $img) {
            // get the classes into an array
            $classes = explode(' ', $img->getAttribute('class')); // this will contain the classes assigned to the element
            if (array_intersect($classes, $classesToReplace))
            {

                // preprocess the image name to match the $values keys
                $imageName = pathinfo($img->getAttribute("src"),PATHINFO_FILENAME);
                if (isset($values[$imageName])) {   
                    if(is_array($values[$imageName])){
                        //Not a text node
                        switch($values[$imageName]['type'])
                        {
                            case 'a':
                                $element = $doc->createElement( 'a',htmlentities($values[$imageName]['value']));
                                $element_href = $doc->createAttribute('href');
                                $element_href->value=htmlentities($values[$imageName]['attr']);
                                $element->appendChild($element_href);
                                if($classF) {
                                    $element_class = $doc->createAttribute('class');
                                    $element_class->value=$classF;
                                    $element->appendChild($element_class);
                                }
                                break;
                            default:{trigger_error("Invalid element type", E_USER_ERROR);}
                        }
                    }
                    else {$element = $doc->createTextNode($escape?htmlentities($values[$imageName]):$values[$imageName]);}
                    $img->parentNode->replaceChild($element,$img);
                }
            }
        }
        $body = $doc->getElementsByTagName('body')->item(0);
        $template=$doc->saveHTML($body);    //Select the body tag 
        $template = str_replace(array('<body>', '</body>'), '', $template);  //strip the body tags
        unset($doc,$xpath);
    }
    return $template;
}

传递给 substituteImg() 的示例数组

Array
(
    [bla] => 2721930660
    [link1] => Array
        (
            [type] => a
            [value] => Yes
            [attr] => javascript:void(0)
        )
    [link2] => Array
        (
            [type] => a
            [value] => link
            [attr] => https://example.com/index.php?foo=123&amp;bar=321
        )
)

最佳答案

你应该使用 &amp;每当你想表达数据&在 HTML 中,除非您在内容明确标记为 CDATA(这意味着 <script><style> 元素)的元素中使用它。

您不应该手动使用&amp;当您使用 DOM API 来操作 DOM 中的文本时。 (这就是你在这里所做的)。

如果 DOM 是从 HTML 文档生成的,&amp;将被解析为 &当您生成 DOM 时。

如果您从 DOM 生成 HTML,&将表示为 &amp;当您将其转换为 HTML 时。


For my specific example, I think it is happening due to mb_convert_encoding(),

不,这是由于 $doc->saveHTML($body);它将 DOM 转换为 HTML。

关于javascript - 何时不应在 HTML 链接中使用 & 实体(和)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869708/

相关文章:

javascript - 正则表达式在 xml 标签中查找属性并替换其值(节点/Javascript)

php - 如何在动态加载 jquery 内容时生成可添加书签的 URL?

php - 找不到类 'App\Http\Controllers\Saml2'

javascript - Vue中如何将img src绑定(bind)到数据

javascript - 无限循环将 Ajax 数据附加到 div

javascript - forEach 的替代方案

php - 为 Wordpress 存储自定义数据的最佳做法是什么

php - 解析错误: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM, expecting T_VARIABLE [duplicate]

javascript - 使用 javascript 或 html 本身清除文本字段

html - 垂直对齐的菜单项