简体   繁体   English

PHP preg_replace两个字符串之间的文本

[英]PHP preg_replace text between two strings

I have a string that would have a similar pattern as $string below (pulled from forum posts). 我有一个字符串,其格式类似于下面的$ string(从论坛帖子中拉出)。

$string = "[quote=\"abc123\":27zslwh3]I don't agree with most of the statements made here.[/quote:27zslwh3] I don't think that is very nice of you.";
$pattern = "/\[quote\=.\*\[\/quote.*\]/";
$replace = "";

$updated_text = preg_replace($pattern,$replace,$string);
echo $updated_text;

I am trying to use preg_replace to remove all the text between the opening and closing [quote] tags and echo the remaining string: "I don't think that is very nice of you." 我正在尝试使用preg_replace删除开始和结束[quote]标记之间的所有文本,并回显其余的字符串:“我觉得这对你不太好。” only (from $string above). 仅(从上面的$ string开始)。

The pattern I am using for the regular expression is supposed to look for the beginning of the opening [quote] tag, then search through until it finds the closing [quote] tag and the final ] of the tag. 我在正则表达式中使用的模式应该先查找开始[quote]标记的开头,然后进行搜索,直到找到该标记的闭合[quote]标记和最后一个]。

The above doesn't seem to work properly though and I am not too proficient with reg expressions so am stuck here. 上面的代码似乎不能正常工作,而且我不太熟悉reg表达式,因此被困在这里。 Any help would be appreciated. 任何帮助,将不胜感激。

.

NOTE: I tried both codes provided by drew010 and bsdnoobz (thanks for the code and explanations) and it still didn't work. 注意:我尝试了drew010bsdnoobz提供的两个代码(感谢代码和说明),但仍然无法正常工作。 The issue is that I did not capture the $string text correctly. 问题是我没有正确捕获$ string文本。 It should read as: 它应显示为:

$string = '[quote="abc123":27zslwh3]I dont agree with most of the statements made here.

abc123[/quote:27zslwh3]
I dont think that is very nice of you.';

there are html chars for the double quotes and what appears to be either new line or carriage return characters as well which is probably what prevented the regular expressions submitted below not work for me. 有用于双引号的html字符,似乎也包括换行符或回车符,这可能是阻止下面提交的正则表达式对我不起作用的原因。

$string = '[quote="abc123":27zslwh3]I don\'t agree with most of the statements made here.[/quote:27zslwh3] I don\'t think that is very nice of you.';
echo preg_replace('/\[quote.+?\].+?\[\/quote.+?\]/is', '',$string);
// will print: I don't think that is very nice of you.

Note: There are both single quote and double quotes in your input string. 注意:输入字符串中同时包含单引号和双引号。 This example wraps the input string with single quote, and escape any single quote within the string. 本示例将输入字符串括在单引号中,并对字符串中的任何单引号进行转义。

Here is a pattern that works also: 这也是一种有效的模式:

$pattern = '/\[quote[^\]]*\].*?\[\/quote[^\]]*\]/is';

It will match formats like what you have, or simply bare quotes like [quote]Text...[/quote] 它将匹配您所拥有的格式,或者仅匹配[quote]Text...[/quote]

To break it down: 分解:

\\[quote[^\\]]*\\] says match text [quote and any character except ] 0 or more times \\[quote[^\\]]*\\]表示匹配文本[quote ] 外的任何字符0次或更多次

\\] matches the closing ] on the end of the opening [quote] tag. \\]匹配闭合]在开口的端部[quote]标签。

.*? matches any character 0 or more times. 匹配任何字符0次或更多次。 The ? ? in this context makes this match ungreedy (meaning it will stop when it matches the next part of the pattern first, rather than last. 在这种情况下,使该匹配不愉快(这意味着,当它首先匹配模式的下一部分而不是最后匹配模式时,它将停止。

\\[\\/quote[^\\]]*\\] then matches [/quote and any character except ] 0 or more times, and finally we consume the closing ] \\[\\/quote[^\\]]*\\]然后匹配[/quote ] 之外的任何字符0次或更多次,最后我们使用结束符]

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

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