php php从字符串中删除第一个<p>和最后一个</p>标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4713794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:06:35  来源:igfitidea点击:

php remove first <p> and last </p> tags from string

php

提问by Ste

How to remove first

首先如何去除

and last

最后

字符串中的标签?示例字符串:

$string = "<p>text text<br>text<p>text</p></p>";

采纳答案by Daan Timmer

$string = "<p>text text<br>text<p>text</p></p>";

$pattern = "=^<p>(.*)</p>$=i";

echo preg_match($pattern, $string, $matches). "<br />" ;
var_dump($matches);

gives me:

给我:

1<br />
array(2) {
  [0]=>
  string(35) "<p>text text<br>text<p>text</p></p>"
  [1]=>
  string(28) "text text<br>text<p>text</p>"
}

回答by Steffen Müller

Try

尝试

$string = preg_replace('/<p[^>]*>(.*)<\/p[^>]*>/i', '', $string);