我有一些文字被包裹在[quote][/quote]
中,我試圖匹配這些標籤之前的所有文本,這些標籤之間的所有內容以及這些標籤之後的所有內容。問題在於它們可能有多次出現,但不在彼此之內。preg_match_all越來越奇怪
我這樣做的原因是因爲我想對這些標記之外的所有文本運行過濾器,無論是否存在多個事件。
這就是我開始一起工作:
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]
)
)
正如你可以看到它沒有得到所需的輸出。任何幫助,將不勝感激。
啊......不是HTML的標記語言 - 肯定的正則表達式最終將正確的工具? – 2012-04-08 00:37:39
我有自定義的bbcode像標籤被分析成HTML。所有的正則表達式解析都是在PHP中完成的。 – 2012-04-08 00:42:53
對不起,我有點諷刺,根據這個[非常流行的謬論](http://stackoverflow.com/a/1732454/596781)。答案是,*不要*使用正則表達式,因爲它們不是正確的工具。 – 2012-04-08 00:45:32