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

时间:2016-03-08 14:14:55

标签: 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
        )
)

1 个答案:

答案 0 :(得分:2)

只要您想在HTML中表达数据&amp;,就应该使用&,除非您在内容明确标记为CDATA的元素中使用它(这意味着{{1} }和<script>元素。)

当您使用DOM API处理DOM中的文本时,不应该手动使用<style>。 (这就是你在这里做的事情)。

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

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

  

就我的具体例子而言,我认为这是因为mb_convert_encoding(),

不,这是由于&amp;将DOM转换为HTML。