I need regex for preg_replace to change:
[quote](content)[/quote]
into <blockquote>(content)</blockquote>
+
[quote=(username)][/quote]
into <blockquote>Post user: (username)</blockquote>
+
[quote=(username);(id)][/quote]
into <blockquote>Post user: (username).<br/>ID: (id)</blockquote>
Ofcourse, the (content), (username), (id) should b开发者_如何学Goe the ones which regex will grep. I've searched alot, and didn't finded any good regex for this action. So, help me guys, please.
I would do it with 3 separate regexes, since it would turn into a giant mess if you tried to put two conditionals inside one regex.
preg_replace('#\[quote\](.+?)\[/quote\]#', '<blockquote>$1</blockquote>', $input);
preg_replace('#\[quote=(.+?)\]\[/quote\]#', '<blockquote>Post user: $1</blockquote>', $input);
preg_replace('#\[quote=(.+?);(.+?)\]\[/quote\]#', '<blockquote>Post user: $1.<br\>ID: $2</blockquote>', $input);
This is taking your question verbatim, of course. It seems like you forgot about the (content) in the second two examples.
EDIT: If you really want to use only one preg_replace
call, you can make arrays containing those patterns and replacements, as seen in the second example here.
EDIT 2: Does BBCode support nested quote blocks? If so, this would be a bit more complicated.
EDIT 3: Even simpler way. Since [/quote]
always leads to </blockquote>
, you can skip all this capture group nonsense, and just replace the open and close tags separately:
preg_replace('#\[quote\]#', '<blockquote>', $input);
preg_replace('#\[quote=(.+?)\]#', '<blockquote>Post user: $1>', $input);
preg_replace('#\[quote=(.+?);(.+?)\]#', '<blockquote>Post user: $1.<br\>ID: $2', $input);
preg_replace('#\[/quote\]#', '</blockquote>', $input);
精彩评论