2017-05-03 25 views
0

使用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&eacute;der &agrave; la fiche</a>' + 
        '</div>'); 
       } 
      } 
     } 
    /* ]]&gt; */ 

回答

1

充分利用[\s\S]*非貪婪,即[\s\S]*?

\/\*\s*<!\[CDATA\[[\s\S]*?\/\*\s*\]\]>\s*\*\/ 

演示:https://regex101.com/r/AutLW9/1

+0

似乎不工作。顯示'正在處理...'沒有結果 – LeMoussel

+0

嗯,我不確定原因。試試這個鏈接:https://regex101.com/r/ZiH3zj/1 –

+0

同樣的錯誤,但它在PHP中確定。我用PHP發佈你的解決方案。 – LeMoussel

0

梅德Egorov PHP解決方案。

$re = '/\/\*\s*<!\[CDATA\[[\s\S]*?\/\*\s*\]\]>\s*\*\//'; 
$str = '<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(); 
    .......................... 
    .......................... 
/* ]]> */ 
</script> 
some text5 ........................'; 
$subst = ''; 

$result = preg_replace($re, $subst, $str); 

echo "The result of the substitution is ".$result; 
0

CData節是一種字符節點,就像文本節點一樣。對於大多數目的,你可以用同樣的方式處理它們 - 區別在於序列化。所以使用XPath獲取節點,並刪除他們,如果他們是CDATA節(而不是文本節點):

$document = new DOMDocument(); 
$document->loadHtml($html); 
$xpath = new DOMXpath($document); 

foreach($xpath->evaluate('//text()') as $section) { 
    if ($section instanceof DOMCDATASection) { 
    $section->parentNode->removeChild($section); 
    } 
} 

echo $document->saveHtml(); 

然而,你可能要重新考慮。沒有CDATA部分真的很重要嗎?您可能需要刪除script元素的內容。這是更短:

$document = new DOMDocument(); 
$document->loadHtml($html); 
$xpath = new DOMXpath($document); 

foreach($xpath->evaluate('//script/node()') as $node) { 
    $node->parentNode->removeChild($section); 
} 

echo $document->saveHtml(); 

//script/node()一個script元素內任何子節點相匹配。無論是CDATA部分,文本節點還是其他任何東西。

+0

Goog解決方案不使用RegExp。但我有一個錯誤。我用它更新我的文章。 – LeMoussel