Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用正则表达式删除标记及其内容?_Php_Regex - Fatal编程技术网

Php 如何使用正则表达式删除标记及其内容?

Php 如何使用正则表达式删除标记及其内容?,php,regex,Php,Regex,$str='some text tag contents more text' 我的问题是: 如何检索介于之间的内容标记内容 及 如何从$str中删除及其内容 我正在使用PHP 谢谢。如果我的\u标签无法嵌套,请尝试此操作以获取匹配项: preg_match_all('/<MY_TAG>(.*?)<\/MY_TAG>/s', $str, $matches) preg_match_all('/(.*?)/s',$str,$matches) 要删除它们,请改用。尽管唯一完

$str='some text tag contents more text'

我的问题是: 如何检索介于
之间的内容
标记内容

如何从
$str
中删除
及其内容

我正在使用PHP


谢谢。

如果
我的\u标签
无法嵌套,请尝试此操作以获取匹配项:

preg_match_all('/<MY_TAG>(.*?)<\/MY_TAG>/s', $str, $matches)
preg_match_all('/(.*?)/s',$str,$matches)

要删除它们,请改用。

尽管唯一完全正确的方法是不使用正则表达式,但如果您接受它不能处理所有特殊情况,您可以得到您想要的:

preg_match("/<em[^>]*?>.*?</em>/i", $str, $match);
// Use this only if you aren't worried about nested tags.
// It will handle tags with attributes
preg_match(“/]*?>.*?/i”,$str,$match);
//仅当您不担心嵌套标记时才使用此选项。
//它将处理带有属性的标记

preg_替换(“/]*?>.*?”、“,$str);

您不想为此使用正则表达式。更好的解决方案是将内容加载到中,并使用DOM树和标准DOM方法对其进行处理:

$document = new DOMDocument();
$document->loadXML('<root/>');
$document->documentElement->appendChild(
    $document->createFragment($myTextWithTags));

$MY_TAGs = $document->getElementsByTagName('MY_TAG');
foreach($MY_TAGs as $MY_TAG)
{
    $xmlContent = $document->saveXML($MY_TAG);
    /* work on $xmlContent here */

    /* as a further example: */
    $ems = $MY_TAG->getElementsByTagName('em');
    foreach($ems as $em)
    {
        $emphazisedText = $em->nodeValue;
        /* do your operations here */
    }
}
$document=新的DOMDocument();
$document->loadXML(“”);
$document->documentElement->appendChild(
$document->createFragment($myTextWithTags));
$MY_TAGs=$document->getElementsByTagName('MY_TAG');
foreach($MY_标记为$MY_标记)
{
$xmlContent=$document->saveXML($MY_标记);
/*在这里处理$xmlContent*/
/*再举一个例子:*/
$ems=$MY_标记->getElementsByTagName('em');
foreach($ems作为$em)
{
$emphasisedText=$em->nodeValue;
/*你在这里做手术吗*/
}
}

对于删除,我只使用了以下命令:

$str = preg_replace('~<MY_TAG(.*?)</MY_TAG>~Usi', "", $str);

$str=preg\u replace(“~我测试了这个函数,它也适用于嵌套的标记,使用true/false排除/包括你的标记。在这里可以找到:


我想知道以下答案在任何给定的一天中被链接了多少次:HTML解析器等等等等…你知道这个练习。@user187580:s标志使
匹配换行符。请看,如果你可能在字符串中多次找到此标记,那么最好使用此模式设置ungreedy。否则你会发现你转换了此stri在
*
之后,将“这是一个非常重要的设置行”改为“这是行”@Don The
。我正确地看了这个答案,没有看到?修饰符,哇!做得好(y)对span很好,例如$ptitle=preg\u replace('~
$str = preg_replace('~<MY_TAG(.*?)</MY_TAG>~Usi', "", $str);
<?php
function strip_tags_content($text, $tags = '', $invert = FALSE) {

  preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
  $tags = array_unique($tags[1]);
   
  if(is_array($tags) AND count($tags) > 0) {
    if($invert == FALSE) {
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
    }
    else {
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
    }
  }
  elseif($invert == FALSE) {
    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
  }
  return $text;
}




// Sample text:
$text = '<b>sample</b> text with <div>tags</div>';

// Result for:
echo strip_tags_content($text);
// text with

// Result for:
echo strip_tags_content($text, '<b>');
// <b>sample</b> text with

// Result for:
echo strip_tags_content($text, '<b>', TRUE);
// text with <div>tags</div>