解析 BBCode 样式标签以访问标签名称和包含的文本
我正在尝试解析一些文本,例如:
$text = "Blah blah [a]findme[/a] and [b]findmetoo[b], maybe also [z]me[/z].";
我现在拥有的是:
preg_match_all("/[*?](.*?)[\/*?]/", $text, $matches);
不幸的是,这不起作用。
有什么想法如何解析、返回节点键和相应的节点值吗?
I'm trying to parse some text for example:
$text = "Blah blah [a]findme[/a] and [b]findmetoo[b], maybe also [z]me[/z].";
What I have now is:
preg_match_all("/[*?](.*?)[\/*?]/", $text, $matches);
Which doesn't work unfortunately.
Any ideas how to parse, return the node key and the corresponding node value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,如果您不将
()
放在*?
周围,则您与标签名称不匹配,其次,使用[*?]
将匹配多个[
直到您想要在内部匹配的]
,因此您应该执行[(.*?)]
和[ \/(.*?)]
您必须尝试以下方法:
这不能保证有效,但会让您更接近。
你也可以这样做:
然后 foreach 结果递归循环直到 preg_match_all 返回 false,这是一种可能的嵌套方式。
Well firstly by you not putting
()
around your*?
your not matching the tag name, and secondly, using[*?]
will match multiple[
until the]
where you want to match inside, so you should be doing[(.*?)]
and[\/(.*?)]
You would have to try something along the lines of:
this is not guaranteed to work but will get you closer.
you could also do:
and then foreach result loop recursively until preg_match_all returns false, that's a possible way how to do nesting.
为了匹配相同的标签,您需要一个反向引用:
这假设没有嵌套,如果您需要嵌套,请告诉我。
顺便说一句,我不知道你要如何处理这个 bbcode 风格的工作,但通常你会想要使用 preg_replace_callback() 来处理此类文本的内联修改,使用与上面类似的正则表达式。
In order to match the same tags, you need a backreference:
This assumes no nesting, if you need nesting then let me know.
Incidentally, I do not know what you are going to do with this bbcode style work, but usually you would want to use preg_replace_callback() to deal with inline modification of this sort of text, with a regexp similar to the above.
尝试:
这应该会为你指明正确的方向。
Try:
That should point you in the right direction.
我想出了这个正则表达式
((\[[^\/]\]).+?(\[\/[^\/]\]))
。希望对你有用I came up with this regex
((\[[^\/]\]).+?(\[\/[^\/]\]))
. Hope will work for you我不是正则表达式猴子,但我认为您需要转义这些括号并创建组进行搜索,因为括号不会返回结果(括号会返回结果):
希望这有效!
I'm no regex monkey, but I think you need to escape those brackets and create groups to search for, as brackets don't return results (parentheses do):
Hope this works!
即使 [b]“标签”未用 [\b] 反斜杠“b”关闭,您的第二个示例是否也应该被捕获。如果标签应该正确关闭,那么使用
这将确保开始和结束标签匹配。
Should your second example also be captured even though the [b] "tag" is not closed with the [\b] backslash 'b'. If tags should be properly closed then use
This will ensure that opening and closing tags match.
您可以尝试以下操作:
查看
所做的更改:
[ 和
]
是正则表达式元字符用于定义字符类。到
匹配文字
[
和]
你需要逃离他们。
换行符)以非贪婪的方式使用
<代码>.*?。
将与其匹配的模式括起来
(..)
以便它们被捕获。You can try this:
See it
Changes made:
[
and]
are regex meta-charactersused to define character class. To
match literal
[
and]
you need toescape them.
newline) in non-greedy way you use
.*?
.enclose the pattern matching it in
(..)
so that they get captured.