PHP中带类的Strip标记

PHP中带类的Strip标记,php,Php,所以我需要去掉classtip的span标记。 那就是和相应的,以及里面的所有东西 我怀疑需要一个正则表达式,但我在这方面做得非常糟糕 笑 <?php $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1}1,$3'; echo preg_replace($pattern, $replacement, $string); ?> 没有错误。。。但是 <?php

所以我需要去掉class
tip
span
标记。 那就是
和相应的
,以及里面的所有东西

我怀疑需要一个正则表达式,但我在这方面做得非常糟糕


<?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;
?>

一个简单的正则表达式,如:

<span class="tip">.+</span>
+
不起作用,问题是如果在tip span中打开和关闭另一个span,您的正则表达式将以其结尾而不是tip结尾终止。基于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;
?>
这是“正确”的方法(改编自)

输入:

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

代码:

loadXML(“$str.”);
//迭代DOM
递归($doc,$doc->documentElement);
//输出结果
foreach($doc->childNodes->item(0)->childNodes as$node){
echo$doc->saveXML($node);
}
?>
输出:

<div>lol wut <span>don't remove!</span></div>
lol-wut不删除!

现在没有regexp,也没有繁重的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);
  }
}
$html=”。。。你好,伙计;
$tag='';
$tag_close='';

$tag_family='Mmmm,正确的方法是使用DOM解析器进行分析-它也适用于您的“及其内部的所有内容”要求。可能的重复我冒昧地将其标记为重复,即使它不是100%。在删除之前,您必须测试所需的标记和类名。这不好。该方法不允许我检查类。我不能删除所有的
span
s。你在说什么?当然,该方法允许您检查类
如果$node->class==“tip”…
并且我不明白为什么它不应该删除所有的
span
s`?好的,那么如何将这个正则表达式应用到我的字符串?XD您可能会使用preg_replace()将匹配的字符串替换为空字符串。不要忘记,您需要正则表达式分隔符,上的示例使用正斜杠作为分隔符。它不起作用。它抱怨在关闭参数时出错(
)…请查看此线程中的第三条注释或我的更新答案。如果标记包含任何其他属性,则此选项将中断。您还忘记了正则表达式分隔符。有效的HTML内容(完整或片段)可能不是XML有效,因此您的分析可能会失败。如果您有一个实际的完整HTML文档,您可以使用
loadHTML
加载它。要不然,硬汉努吉。
<div>lol wut <span>don't remove!</span></div>
$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);
  }
}