【问题标题】:Tidy breaks anchor hrefs整洁的锚点hrefs
【发布时间】:2013-11-28 19:58:27
【问题描述】:

我有以下 HTML 代码(我无法更改,它来自外部源)

<a href="http://linkhref.com"><center>Link Text</center></a>

但是经过 tidy 处理后,HTML 被破坏(锚点不可点击,因为 innerHTML 被附加在标签之后:( 我正在使用以下配置选项:

$config = array(
            'output-xml'=>true,
            'wrap'=>false,
            'doctype'=>'omit',
            'quote-nbsp'=>false,
            'quiet'=>true,
            'bare'=>true,
            'fix-backslash'=>false,
            'indent-cdata'=>false
    );

整洁会输出:

<html> 
<head> 
<title></title> 
</head> 
<body> 
<a href="http://linkhref.com"></a> 
<center>Link Text</center> 
<br /> 
</body> 
</html>

有什么建议吗?非常感谢。

【问题讨论】:

    标签: php anchor href tidy


    【解决方案1】:

    理想情况下,您不希望“中心”标签保留 - 它在 HTML4 中已被弃用,在 HTML5 中不再受支持。

    尝试添加配置选项:

    'clean' => true
    

    【讨论】:

    • 嗨,我试过了 :) 但唯一改变的想法是:
      Text
    • 也许值得进行预处理以删除“中心”标签。 IE。执行 str_replace 以删除或用带有样式的跨度替换它们。
    • 好吧,这不是我想要的解决方案,但它似乎是唯一的解决方案。为了不破坏太多 HTML 的
      标签,我使用了以下删除:$string = preg_replace('# (]*>)
      (.*?)
      #si','$1$2',$string); (仅替换 center ,即标记的直接子级)
    • 更好地说,您的回答帮助我选择了解决方案。我希望我能找到一种使用 Tidy 的方法,但是在解析后它就坏了,所以我认为这是一个 Tidy Bug。您的回答确保我采取了解决方法。不过还是非常感谢...
    【解决方案2】:

    A 标签也有类似的问题,现在在 HTML 5 中允许作为块元素。

    我是这样解决的:

    static function tidy_links_cb($m)
    {
        $blocks = 'div|ul|li|dl|form|fieldset|mena|nav|table|tr|td|th|address|article|aside|blockquote|dir|div|dl|fieldset|footer|form|h1|h2|h3|h4|h5|h6|header|hr|menu|nav|ol|p|pre|section|table|ul';
        if (preg_match('~<('.$blocks.')~is', $m[1])) {
             // THIS LINK CONTAINS BLOCK ELEMENT
            return '<alink'.$m[1].'</alink>';
        }
        return $m[0];
    }
    
    static function Tidy($html)
    {
        $config = array(
            'wrap' => 0,
            'show-body-only' => true,
            'enclose-text' => true,
            'output-xhtml' => true,
            'doctype' => 'omit',
            'bare' => true,
            'char-encoding' => 'raw',
            'input-encoding' => 'raw',
            'output-encoding' => 'raw',
            'quiet' => true,
            'hide-comments' => true,
            'new-blocklevel-tags' => 'section alink',
            'new-inline-tags' => 'button',
            'drop-empty-elements' => false,
        );
    
       // you cannot simply replace all <A> tags because Tidy would mess up the inline ones
        $html = preg_replace_callback('~<a(.*)</a>~isU', array(self, 'tidy_links_cb'), $html);
    
        $html = tidy_parse_string($html, $config);
        tidy_clean_repair($html);
    
        $html = str_replace('<alink ', '<a ', $html);
        $html = str_replace('alink>', 'a>', $html);
    
        $html = (string) $html;
        return $html;
    }
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签