使用PHP,在HTML文件中,我想要刪除腳本元素中的CDATA
塊。如何刪除腳本元素中的CDATA塊?
<script type="text/javascript">
/* <![CDATA[ */
var A=new Array();
..........................
..........................
/* ]]> */
</script>
some text2 ........................
some text3 ........................
some text4 ........................
<script type="text/javascript">
/* <![CDATA[ */
var B=new Array();
..........................
..........................
/* ]]> */
some text5 ........................
我還沒有找到如何選擇&刪除此節點使用XPath & PHP DomDocument。
我試着用這個正則表達式 $re = '/\/\*\s*<!\[CDATA\[[\s\S]*\/\*\s*\]\]>\s*\*\//i';
但這消除包括CDATA的2塊之間的一個所有文本。
因此,我得到一個空字符串,而不是
some text2 ........................
some text3 ........................
some text4 ........................
some text5 ........................
任何想法?
更新與THW的解決方案:
有了這個頁面,看來,CDATA段的文本沒有得到很好的解析
libxml_use_internal_errors(true);
$domDoc = new DOMDocument();
$domDoc->loadHTMLFile('https://www.maisons-qualite.com/le-reseau-mdq/recherche-constructeurs-agrees/construction-maison-neuve-centre-val-loire');
libxml_clear_errors();
$xpath = new DOMXpath($domDoc);
foreach($xpath->evaluate('//text()') as $section) {
if ($section instanceof DOMCDATASection) {
print_r($section->textContent);
$section->parentNode->removeChild($section);
}
}
$content = $domDoc->saveHTML();
我得到這個的textContent
.....
.....
function updateConstructeurs(list) {
for (var i in list) {
if(list[i]['thumbnail']) {
jQuery('#reseau-constructeurs').append('<div class="reseau-constructeur">' +
'<div class="img" style="background-image:url(' + list[i]['thumbnail'] + ')">
爲
function updateConstructeurs(list) {
for (var i in list) {
if(list[i]['thumbnail']) {
jQuery('#reseau-constructeurs').append('<div class="reseau-constructeur">' +
'<div class="img" style="background-image:url(' + list[i]['thumbnail'] + ')"></div>' +
'<h3>' + list[i]['title'] + '</h3>' +
'<a class="btn purple" href="' + list[i]['link'] + '">Accéder à la fiche</a>' +
'</div>');
}
}
}
而作爲一個結果,而不是得到一個空字符串,我們有:
'<h3>' + list[i]['title'] + '</h3>' +
'<a class="btn purple" href="'%20+%20list%5Bi%5D%5B'link'%5D%20+%20'">Accéder à la fiche</a>' +
'</div>');
}
}
}
/* ]]> */
似乎不工作。顯示'正在處理...'沒有結果 – LeMoussel
嗯,我不確定原因。試試這個鏈接:https://regex101.com/r/ZiH3zj/1 –
同樣的錯誤,但它在PHP中確定。我用PHP發佈你的解決方案。 – LeMoussel