PHP DOMDocument按标记名称获取元素会忽略已注释的元素

时间:2017-02-16 09:16:02

标签: php html domdocument

我有一个函数可以将查询字符串附加到HTML文件中找到的每个<a>标记。在该文件中,我有一些<a>标签已注释,我也需要定位。

HTML

<!-- Commented link
    <a class="legal_link" target="_blank" href="https://commented-url.com">some text</a>
-->

<a href="https://url.com" target="_blank" class="btn_center">
    Some text
</a>

<!--[if mso]>
<v:roundrect xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="urn:schemas-microsoft-com:office:word" href="https://another-commented-url.com" target="_blank" style="height:55px;v-text-anchor:middle;width:300px;" arcsize="10%" stroke="f" fillcolor="#f7e7e7">
<w:anchorlock/>
<center>
<![endif]-->
    <a href="https://url.com" target="_blank" class="btn_center">
        Some text
    </a>
<!--[if mso]>
</center>
</v:roundrect>
<![endif]-->

PHP函数

//Append query string to every a element
//-------------------------------------------------------------------------------------------------------------------------//
function appendQueryString($htmlContent, $queryString){
    $dom = new DOMDocument();
    $dom->loadHTML($htmlContent, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $htmlElements = $dom->getElementsByTagName('a'); // Not getting commented tags


    foreach($htmlElements as $htmlElement){
        $htmlElementUrl = $htmlElement->getAttribute('href');
        $htmlElementUrlParsed = parse_url($htmlElementUrl);
        if($htmlElementUrlParsed['path'] == null){ $htmlElementUrl .= '/'; }
        $htmlElementUrlSeparator = ($htmlElementUrlParsed['query'] == NULL) ? '?' : '&';
        $htmlElementUrl = $htmlElementUrl . $htmlElementUrlSeparator . $queryString;
        $htmlElement->setAttribute('href', $htmlElementUrl);
    }

    return $dom->saveHTML();
}

0 个答案:

没有答案