Javascript 什么时候不应该在HTML链接中使用ampersands实体?

Javascript 什么时候不应该在HTML链接中使用ampersands实体?,javascript,php,html,dom,html-entities,Javascript,Php,Html,Dom,Html Entities,什么时候不应该在HTML链接中使用ampersands实体(&;) 上下文:我询问的原因是我正在使用DOMDocument()将标记转换为不同的HTML,并且正在复制符号。对于我的特定示例,我认为这是由于mb\u convert\u encoding()造成的,但是如果我不使用它,我会遇到其他问题。也许还有其他时候不应该在HTML链接中使用符号和实体 您应该使用&当您想用HTML表达数据&时,除非您在一个元素中使用它,该元素的内容被明确标记为CDATA(这意味着和元素) 您不应手动使

什么时候不应该在HTML链接中使用ampersands实体(
&;

上下文:我询问的原因是我正在使用
DOMDocument()
标记转换为不同的HTML,并且正在复制符号。对于我的特定示例,我认为这是由于
mb\u convert\u encoding()
造成的,但是如果我不使用它,我会遇到其他问题。也许还有其他时候不应该在HTML链接中使用符号和实体


您应该使用
&
当您想用HTML表达数据
&
时,除非您在一个元素中使用它,该元素的内容被明确标记为CDATA(这意味着
元素)

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

如果DOM是从HTML文档生成的,
&将被解析为
&

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


对于我的特定示例,我认为这是由于mb_convert_encoding()造成的


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

谢谢。回答得很好。因此,有两次不应该使用
&
是在CDATA中,并且在使用DOM API时。你能想到其他任何时候吗?基本上,任何时候你正在处理一些不希望你编写原始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;
}
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
        )
)