Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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:Regex替换字符串/HTML标记之间的所有内容_Php_Regex - Fatal编程技术网

PHP:Regex替换字符串/HTML标记之间的所有内容

PHP:Regex替换字符串/HTML标记之间的所有内容,php,regex,Php,Regex,我有下面的文本,但希望从字符串中删除引号位,我使用下面的正则表达式,但它给了我下面的错误 文本示例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

我有下面的文本,但希望从字符串中删除引号位,我使用下面的正则表达式,但它给了我下面的错误

文本示例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>

我已经了解了哪些有帮助,但我还没有弄清楚,任何帮助都非常感谢。

从HTML中提取文本是很棘手的,所以最好的选择是使用类似Html2Text的库。它是专门为此目的建造的

使用composer安装:

编写器需要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.
$html=new\Html2Text\Html2Text(“[quote

文本的测试件[/quote]

这是一个测试。”); echo$html->getText();//测试文本这是一个测试。
或者您可以简单地使用函数PHP strip_tags

string strip_标记(string$str[,string$allowed_标记])

echo str_replace(“[/quote]”)、str_replace(“[quote]”)、strip_标签(“
[引用]

测试文本 [quote]

这是一个测试;
出现错误的原因是您没有在正则表达式中转义开头的
[
字符。请参阅我在下面标记的
[

preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
                                                 ^
preg\u replace('/\\[quote\]\[\s\s]+?\[\/quote\]\/',''.$string);
^
这导致启动了一个尚未关闭的字符类。您只需像这样转义这个大括号:

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

生成的文本应该是什么?1.)错误表明您没有在…
\[
..2]中逃逸
[
。)您的示例文本包含
[quote
而不是
[quote]
。此外,没有必要逃逸
@mrzasa,抱歉,现在更新了问题。此工具将帮助您测试正则表达式(解释得很好):
Warning: preg_replace(): Compilation failed: missing terminating ] for character class at offset
$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.
echo str_replace("[/quote]","",str_replace("[quote","",strip_tags("<p>
[quote</p>
test piece of text
<p>[/quote]</p>
This is a test.")));
preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>[\/quote\]\<\/p\>/', '', $string);
                                                 ^
preg_replace('/\<p\>\[quote\]\<\/p\>[\s\S]+?\<p\>\[\/quote\]\<\/p\>/', '', $string);