2011-04-12 48 views
2

所以我需要去掉類tipspan標籤。 因此,這將是它裏面<span class="tip">和相應的</span>,一切...帶類中的帶狀標籤在PHP中

我懷疑是需要一個正則表達式,但我在此非常鬧心。


笑...

<?php 
$string = 'April 15, 2003'; 
$pattern = '/(\w+) (\d+), (\d+)/i'; 
$replacement = '${1}1,$3'; 
echo preg_replace($pattern, $replacement, $string); 
?> 

沒有給出錯誤......但

<?php 
$str = preg_replace('<span class="tip">.+</span>', "", '<span class="rss-title"></span><span class="rss-link">linkylink</span><span class="rss-id"></span><span class="rss-content"></span><span class=\"rss-newpost\"></span>'); 
echo $str; 
?> 

給我的錯誤:

Warning: preg_replace() [function.preg-replace]: Unknown modifier '.' in <A FILE> on line 4 

以前,錯誤是在在第二行);,但現在.... >>

+0

嗯,*正確的*方式會用DOM解析器來做 - 它也適用於你的「及其中的一切」要求。 – 2011-04-12 19:09:36

+0

[可遞歸循環DOM樹並移除不需要的標記?](http://stackoverflow.com/questions/4562769/recursively-loop-through-the-dom-tree-and-remove-unwanted-tags)我冒昧地將此標記爲重複,即使它不是100%。在刪除之前,您必須測試所需的標記和類名。 – 2011-04-12 19:10:27

+0

這並不好。該方法不允許我檢查課程。我無法刪除所有的「span」。 – Vercas 2011-04-12 19:21:59

回答

1

一個簡單的正則表達式,如:。

<span class="tip">.+</span> 

不會工作,問題是如果另一跨度開了,尖端範圍內關閉,你的正則表達式將會與它的結局結束,而不是提示一個。像註釋中鏈接的基於DOM的工具將提供更可靠的答案。

根據我的評論,您需要在PHP中使用正則表達式時添加模式分隔符。

<?php 
$str = preg_replace('\<span class="tip">.+</span>\', "", '<span class="rss-title"></span><span class="rss-link">linkylink</span><span class="rss-id"></span><span class="rss-content"></span><span class=\"rss-newpost\"></span>'); 
echo $str; 
?> 

可能是適度更大的成功。請查看相關功能的文檔頁面。

+0

這可能會起作用,因爲我內部沒有其他垃圾郵件! – Vercas 2011-04-12 19:25:58

+0

好吧,那麼如何將這個正則表達式應用到我的字符串?xD – Vercas 2011-04-12 19:50:24

+0

您可能會使用preg_replace()將無匹配的字符串替換爲空字符串。不要忘記,您需要正則表達式分隔符,http://us3.php.net/preg_replace上的示例使用正斜槓作爲分隔符。 – preinheimer 2011-04-12 19:51:56

1

這是「正確」的方法(改編自this answer)。

輸入:

<?php 
$str = '<div>lol wut <span class="tip">remove!</span><span>don\'t remove!</span></div>'; 
?> 

代碼:

<?php 
function recurse(&$doc, &$parent) { 
    if (!$parent->hasChildNodes()) 
     return; 

    for ($i = 0; $i < $parent->childNodes->length;) { 
     $elm = $parent->childNodes->item($i); 
     if ($elm->nodeName == "span") { 
     $class = $elm->attributes->getNamedItem("class")->nodeValue; 
     if (!is_null($class) && $class == "tip") { 
      $parent->removeChild($elm); 
      continue; 
     } 
     } 

     recurse($doc, $elm); 
     $i++; 
    } 
} 

// Load in the DOM (remembering that XML requires one root node) 
$doc = new DOMDocument(); 
$doc->loadXML("<document>" . $str . "</document>"); 

// Iterate the DOM 
recurse($doc, $doc->documentElement); 

// Output the result 
foreach ($doc->childNodes->item(0)->childNodes as $node) { 
    echo $doc->saveXML($node); 
} 
?> 

輸出:

<div>lol wut <span>don't remove!</span></div> 
+1

有效的HTML內容(完整或片段)可能不是XML有效的,因此您的解析可能會失敗。 – Skrol29 2011-04-14 00:05:37

+0

如果您有完整的HTML文檔,可以使用'loadHTML'加載它。其他的,堅韌的noogie。 – 2011-04-14 00:07:12

0

現在沒有正則表達式,沒有沉重的XML解析:

$html = ' ... <span class="tip"> hello <span id="x"> man </span> </span> ... '; 
$tag = '<span class="tip">'; 
$tag_close = '</span>'; 
$tag_familly = '<span'; 

$tag_len = strlen($tag); 

$p1 = -1; 
$p2 = 0; 
while (($p2!==false) && (($p1=strpos($html, $tag, $p1+1))!==false)) { 
    // the tag is found, now we will search for its corresponding closing tag 
    $level = 1; 
    $p2 = $p1; 
    $continue = true; 
    while ($continue) { 
    $p2 = strpos($html, $tag_close, $p2+1); 
    if ($p2===false) { 
     // error in the html contents, the analysis cannot continue 
     echo "ERROR in html contents"; 
     $continue = false; 
     $p2 = false; // will stop the loop 
    } else { 
     $level = $level -1; 
     $x = substr($html, $p1+$tag_len, $p2-$p1-$tag_len); 
     $n = substr_count($x, $tag_familly); 
     if ($level+$n<=0) $continue = false; 
    } 
    } 
    if ($p2!==false) { 
    // delete the couple of tags, the farest first 
    $html = substr_replace($html, '', $p2, strlen($tag_close)); 
    $html = substr_replace($html, '', $p1, $tag_len); 
    } 
}