【发布时间】:2015-07-07 08:20:14
【问题描述】:
我正在制作从 phpbb 到 php native 的论坛,我需要解析一些带有 uid 的 bbcode 标签。这是将其解析为不带 uid 的常规 bbcode 的代码:
$regex = "#\[quote:(.*)=(.*)\](.+)\[/quote:(.+)\]#isU";
$text = "outside sample
[quote:c1891a7ad3]
text with link https://www.facebook.com/groups/35688476100/?fref=ts [/quote:c1891a7ad3]
outside text
[quote:c1891a7ad3="Budi"]
written by me , - budi
[/quote:c1891a7ad3]"
preg_replace($regex,"[quote=$2]$3[\quote]",$text);
但结果不是
"outside sample
[quote:c1891a7ad3]
text with link https://www.facebook.com/groups/35688476100/?fref=ts [/quote:c1891a7ad3]
outside text
[quote="Budi"]
written by me , - budi
[\quote]"
应该如何修改regex 以产生预期的结果?
【问题讨论】:
-
您的正则表达式不匹配,因为输入文本 (
[\quote:c1891a7ad3]) 和正则表达式模式中的/中有反斜杠。修复模式或输入字符串。如果您需要在模式末尾匹配[/quote],请修复输入并尝试"#\[quote:([^]]*)=([^]]*)\](.+)\[/quote:([^]]+)\]#isU"regex。 -
感谢“\”的错字 xD aaah 感谢您帮助我解决此问题:3 emm.. (.*) 和 ([^]]*) 或反引号之间有什么不同([^]]+) ?无论如何,谢谢 :D 你在哪里学习正则表达式?我想像你一样了解和学习! :D
-
我上面的评论能解决你的问题吗?
-
是的!你能回答我的评论吗?
-
试试这个regex101.com 来玩弄正则表达式(每个正则表达式都有详细解释,您还可以查看标准正则表达式库)。