0

我有这段文字,我正在尝试删除所有内部引号,只保留一个引用级别。引号内的文本包含任何字符,甚至换行符等。这可能使用正则表达式还是我必须编写一个小解析器?

[quote=foo]I really like the movie. [quote=bar]World 

War Z[/quote] It's amazing![/quote]
This is my comment.
[quote]Hello, World[/quote]
This is another comment.
[quote]Bye Bye Baby[/quote]

这是我想要的文字:

[quote=foo]I really like the movie.  It's amazing![/quote]
This is my comment.
[quote]Hello, World[/quote]
This is another comment.
[quote]Bye Bye Baby[/quote]

这是我在 PHP 中使用的正则表达式:

%\[quote\s*(=[a-zA-Z0-9\-_]*)?\](.*)\[/quote\]%si

我也尝试了这个变体,但它不匹配.,或者,我不知道在引用中还能找到什么:

%\[quote\s*(=[a-zA-Z0-9\-_]*)?\]([\w\s]+)\[/quote\]%i

问题出在此处:

(.*)
4

3 回答 3

2

你可以使用这个:

$result = preg_replace('~\G(?!\A)(?>(\[quote\b[^]]*](?>[^[]+|\[(?!/?quote)|(?1))*\[/quote])|(?<!\[)(?>[^[]+|\[(?!/?quote))+\K)|\[quote\b[^]]*]\K~', '', $text);

细节:

\G(?!\A)              # contiguous to a precedent match
(?>                   ## content inside "quote" tags at level 0
  (                    ## nested "quote" tags (group 1)
    \[quote\b[^]]*]
    (?>                ## content inside "quote" tags at any level
      [^[]+
     |                  # OR
      \[(?!/?quote)
     |                  # OR
      (?1)              # repeat the capture group 1 (recursive)
    )*
    \[/quote]
  )
 |
  (?<!\[)           # not preceded by an opening square bracket
  (?>              ## content that is not a quote tag
    [^[]+           # all that is not a [
   |                # OR
    \[(?!/?quote)   # a [ not followed by "quote" or "/quote"
  )+\K              # repeat 1 or more and reset the match
)
|                   # OR
\[quote\b[^]]*]\K   # "quote" tag at level 0 
于 2013-09-12T02:45:07.040 回答
0

使用这种模式

\[quote=?[^\]]*\][^\[]*\[/quote\](?=((.(?!\[q))*)\[/)

并替换为本示例中的任何内容

于 2013-09-12T04:26:56.963 回答
0

我认为编写解析器会更容易。

使用正则表达式查找[quote][\quote],然后分析结果。

preg_match_all('#(\[quote[^]]*\]|\[\/quote\])#', $bbcode, $matches, PREG_OFFSET_CAPTURE);
$nestlevel = 0;
$cutfrom = 0;
$cut = false;
$removed = 0
foreach($matches(0) as $quote){
    if (substr($quote[0], 0, 1) == '[') $nestlevel++; else $nestlevel--;
    if (!$cut && $nestlevel == 2){ // we reached the first nested quote, start remove here
        $cut = true;
        $cutfrom = $quote[1];
    }
    if ($cut && $nestlevel == 1){ // we closed the nested quote, stop remove here
        $cut = false;
        $bbcode = substr_replace($bbcode, '', $cutfrom - $removed, $quote[1] + 8 - $removed); // strlen('[\quote]') = 8
        $removed += $quote[1] + 8 - $cutfrom;
    }
);
于 2013-09-12T03:00:22.060 回答