解析 BBCode 样式标签以访问标签名称和包含的文本

发布于 2024-10-06 17:47:18 字数 276 浏览 5 评论 0原文

我正在尝试解析一些文本,例如:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

吝吻 2024-10-13 17:47:18

首先,如果您不将 () 放在 *? 周围,则您与标签名称不匹配,其次,使用 [*?] 将匹配多个 [ 直到您想要在内部匹配的 ] ,因此您应该执行 [(.*?)][ \/(.*?)]

您必须尝试以下方法:

/\[(.*?)\](.*?)\[\/(.*?)\]/is

这不能保证有效,但会让您更接近。

你也可以这样做:

/\[(.*?)\](.*?)\[\/\1\]/is

然后 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:

/\[(.*?)\](.*?)\[\/(.*?)\]/is

this is not guaranteed to work but will get you closer.

you could also do:

/\[(.*?)\](.*?)\[\/\1\]/is

and then foreach result loop recursively until preg_match_all returns false, that's a possible way how to do nesting.

世态炎凉 2024-10-13 17:47:18

为了匹配相同的标签,您需要一个反向引用:

这假设没有嵌套,如果您需要嵌套,请告诉我。

$matches = array();
if (preg_match_all('#\[([^\]]+)\](.+?)\[/\1\]#', $text, $matches)) {
   // $matches[0] - entire matched section
   // $matches[1] - keys
   // $matches[2] - values
}

顺便说一句,我不知道你要如何处理这个 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.

$matches = array();
if (preg_match_all('#\[([^\]]+)\](.+?)\[/\1\]#', $text, $matches)) {
   // $matches[0] - entire matched section
   // $matches[1] - keys
   // $matches[2] - values
}

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.

萌辣 2024-10-13 17:47:18

尝试:

$pattern = "/\[a\](.*?)\[\/a\]/";
$text = "Blah blah [a]findme[/a] and [b]findmetoo[b], maybe also [z]me[/z].";
preg_match_all($pattern, $text, $matches);

这应该会为你指明正确的方向。

Try:

$pattern = "/\[a\](.*?)\[\/a\]/";
$text = "Blah blah [a]findme[/a] and [b]findmetoo[b], maybe also [z]me[/z].";
preg_match_all($pattern, $text, $matches);

That should point you in the right direction.

离去的眼神 2024-10-13 17:47:18

我想出了这个正则表达式 ((\[[^\/]\]).+?(\[\/[^\/]\]))。希望对你有用

I came up with this regex ((\[[^\/]\]).+?(\[\/[^\/]\])). Hope will work for you

还在原地等你 2024-10-13 17:47:18

我不是正则表达式猴子,但我认为您需要转义这些括号并创建组进行搜索,因为括号不会返回结果(括号会返回结果):

preg_match_all("/\\[(*?)\\](.*?)\\[\(\/*?)\\]/", $text, $matches);

希望这有效!

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):

preg_match_all("/\\[(*?)\\](.*?)\\[\(\/*?)\\]/", $text, $matches);

Hope this works!

痴者 2024-10-13 17:47:18

即使 [b]“标签”未用 [\b] 反斜杠“b”关闭,您的第二个示例是否也应该被捕获。如果标签应该正确关闭,那么使用

/\[(.*?)\](.*?)\[\/\1\]/

这将确保开始和结束标签匹配。

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

/\[(.*?)\](.*?)\[\/\1\]/

This will ensure that opening and closing tags match.

给不了的爱 2024-10-13 17:47:18

您可以尝试以下操作:

preg_match_all("/\[(.*?)\](.*?)\[\/?.*?\]/", $text, $matches);

查看

所做的更改:

  • [ 和 ] 是正则表达式元字符
    用于定义字符类。到
    匹配文字 [] 你需要
    逃离他们。
  • 匹配任意文本(不带
    换行符)以非贪婪的方式使用
    <代码>.*?。
  • 要匹配节点密钥,您需要
    将与其匹配的模式括起来
    (..) 以便它们被捕获。

You can try this:

preg_match_all("/\[(.*?)\](.*?)\[\/?.*?\]/", $text, $matches);

See it

Changes made:

  • [ and ] are regex meta-characters
    used to define character class. To
    match literal [ and ] you need to
    escape them.
  • To match any arbitrary text(without
    newline) in non-greedy way you use
    .*?.
  • To match the node key you need to
    enclose the pattern matching it in
    (..) so that they get captured.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文