简体   繁体   English

PHP:正则表达式将所有内容替换为字符串/ HTML标记

[英]PHP: Regex replace everything between to strings/HTML tags

I have the below text but want the quote bit removed from the string, I'm using the below regex but it gives me the below error. 我有下面的文本,但想要从字符串中删除引号位,我正在使用下面的正则表达式,但它给了我下面的错误。

Text Example 1 文字范例1

<p>[quote]</p>
<p>[quote]</p>
<p>inner quote text</p>
<p>[/quote]</p>
<p>outer quote text</p>
<p>[/quote]</p>
<p>This is a test.</p>

Text Example 2 文字范例2

<p>[quote][quote]</p>
<p>inner quote text</p>
<p>[/quote]</p>
<p>outer quote text</p>
<p>[/quote]</p>
<p>This is a test.</p>

Expected Text 预期文字

<p>This is a test.</p>

Regex 正则表达式

preg_replace('/<p>\[quote\][\s\S]+?<p>\[\/quote\]<\/p>/', '', $string);

Error 错误

Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset

I've had a look at Deleting text between two strings in php using preg_replace which has helped but I haven't been able to figure it out, any help greatly appreciated. 我曾经看过使用preg_replace在php删除两个字符串之间的文本,这很有帮助 ,但是我一直无法弄清楚,任何帮助都非常感谢。

The reason you're getting the error is because you've not escaped an opening [ character in your regular expression. 出现此错误的原因是,您没有在正则表达式中转义了[字符。 Please see the [ I have marked below: 请参阅[我已在下面标记:

preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
                                                 ^

This has resulted in starting a character class that has not been closed. 这导致启动尚未关闭的字符类。 You should simply escape this opening brace like this: 您应该像这样简单地逃避这个左括号:

preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>\[\/quote\]\<\/p\>/', '', $string);

Extracting text from HTML is tricky, so the best option is to use a library like Html2Text. 从HTML提取文本非常棘手,因此最好的选择是使用类似Html2Text的库。 It was built specifically for this purpose. 它是专门为此目的而构建的。

https://github.com/mtibben/html2text https://github.com/mtibben/html2text

Install using composer: 使用composer安装:

composer require html2text/html2text Basic usage: 作曲家需要html2text / html2text基本用法:

$html = new \Html2Text\Html2Text('<p>[quote</p>test piece of text<p>[/quote]</p>This is a test.');

echo $html->getText();  // test piece of text This is a test.

OR you can use simply the function PHP strip_tags 或者,您可以仅使用函数PHP strip_tags

string strip_tags ( string $str [, string $allowable_tags ] ) 字符串strip_tags(字符串$ str [,字符串$ allowable_tags])

http://php.net/strip_tags http://php.net/strip_tags

echo str_replace("[/quote]","",str_replace("[quote","",strip_tags("<p>
[quote</p>
test piece of text
<p>[/quote]</p>
This is a test.")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM