2011-04-12 46 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); 
    } 
}