0

我有一些包含在其中的文本,[quote][/quote]我正在尝试匹配这些标签之前的所有文本、这些标签之间的所有内容以及这些标签之后的所有内容。问题是它们可能会多次出现,但彼此之间不会出现。

我这样做的原因是因为我想对这些标签之外的所有文本运行过滤器,无论是否出现多次。

这就是我开始使用的内容:

preg_match_all("/(^.*)\[quote\](.*?)\[\/quote\](.*)/si", $reply['msg'], $getthequotes);

这是输出:

Array
(
[0] => Array
    (
        [0] => putting some stuff before the quote
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

yep

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA

adding a quote

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

[i]04/07/12 20:18:07: Edited by Logan(2)[/i]
    )

[1] => Array
    (
        [0] => putting some stuff before the quote

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

yep

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA

adding a quote


    )

[2] => Array
    (
        [0] => [b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i]
    )

[3] => Array
    (
        [0] => 

[i]04/07/12 20:18:07: Edited by Logan(2)[/i]
    )

)

如您所见,它没有得到所需的输出。任何帮助,将不胜感激。

4

2 回答 2

1

我没有尝试过,但你只想要 before[quote]和 after的东西[/quote],你可以为第一次出现的开头引号标签做一个 strpos。现在你知道之前的一切都没有被引用。

接下来,您可以使用 strpos 从第一个匹配引号标记的索引开始查找结束引号标记。你可以丢弃这些东西。

现在使用您刚刚找到的结束引号标记的起始位置为下一个引号块执行另一个 strpos。您可以重复此操作,直到完成。

于 2012-04-08T02:37:45.373 回答
0

可以完成,但您需要对字符串进行多次传递。

$string = 'putting some stuff before the quote
[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

yep

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA

adding a quote

[quote][b]Logan said[/b][br]This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

[i]04/07/12 20:18:07: Edited by Logan(2)[/i]putting some stuff before the quote

[quote][b]Logan said[/b][br]testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA[br][br]did it work?[br][br][i]04/04/12 23:48:46: Edited by Logan(2)[/i][br][br][i]04/04/12 23:55:44: Edited by Logan(2)[/i][/quote]

yep

http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA

adding a quote';

//get rid of whitespace
$string = preg_replace('%\s\s?%', " ",$string);
//break the string on a common element
$pieces =  preg_split('%\[%',$string);
//now discard the elements that are tags
foreach($pieces as $key=>$value):
    $value = trim($value);
    if(strrpos($value,"]") == (strlen($value) -1)):
        unset($pieces[$key]);
    endif;
endforeach;
print_r($pieces);
//and finally strip out the tag fragments
foreach($pieces as $key=>$value):
    $pieces[$key] = preg_replace('%.*]%',"",$value);
endforeach;

结果是一个如下所示的数组:

Array
(
    [0] => putting some stuff before the quote 
    [2] => Logan said
    [4] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA
    [6] => did it work?
    [9] => 04/04/12 23:48:46: Edited by Logan(2)
    [13] => 04/04/12 23:55:44: Edited by Logan(2)
    [15] =>  yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote 
    [17] => Logan said
    [19] => This is the start of the second quote http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA
    [21] => did it work?
    [24] => 04/04/12 23:48:46: Edited by Logan(2)
    [28] => 04/04/12 23:55:44: Edited by Logan(2)
    [31] => 04/07/12 20:18:07: Edited by Logan(2)
    [32] => putting some stuff before the quote 
    [34] => Logan said
    [36] => testing this youtube link http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA
    [38] => did it work?
    [41] => 04/04/12 23:48:46: Edited by Logan(2)
    [45] => 04/04/12 23:55:44: Edited by Logan(2)
    [47] =>  yep http://www.youtube.com/watch?v=8UVNT4wvIGY&feature=g-music&context=G2db8219YMAAAAAAAAAA adding a quote
)
于 2012-04-08T03:45:37.257 回答