【问题标题】:Parse bbcode with arrays用数组解析 bbcode
【发布时间】:2017-07-29 21:40:01
【问题描述】:

以下问题: 上面“列表”中的“li”标签设置不正确,然后它就是。 我试图改变表达,但我还没有找到解决办法。

这是我的(缩短的)代码(以及所有其他遵循相同原理图的“bb”)

function bb($content) {
$search = array (
'#\[list\](.+)\[\/list\]#iUs',
'#\[list=1\](.+)\[\/list\]#iUs',
'#\[\*\](.*)\[/\*\]#iUs',
'#\[IMG\](.+)\[\/IMG\]#iUs'
);
$replace = array (
'<ul>$1</ul>',
'<ol>$1</ol>',
'<li>$1</li>',
'<img src="$1"/>' 
);
$newtext = preg_replace($search, $replace, $content);
$newtext = nl2br($newtext);
$newtext = preg_replace('#<br />(\s*<br />)+#', '<br />', $newtext);
return $newtext;
}
print autolink(bb($NewsItemContent)); // http/s autolinking (later I'll add link preview)

内容将如下所示:

 [h1]MAIN FEATURES[/h1] [list] [*]Doubles https://steamcommunity.com/games/227300/announcements/detail/1294067099912833659 [*]Background screen options [*]Changed light flares on the player and AI vehicles https://steamcommunity.com/games/227300/announcements/detail/1335730452506903011 [/list] [h1]MINOR CHANGES[/h1] [list][*]Auxiliary brakes system support (engine brake and retarder in one control element)[/*][*]Fixed licence plate change in states/countries where no city formats exist[/*][*]Fixed brake vs parking brake behavior[/*][*]Fixed tire noise of silent tires[/*][*]Retarder improvement (better cruise control behavior, no braking with throttle, icon when moving only)[/*][*]Steam Inventory support to allow for distributing rewards from upcoming World of Trucks events[/*][/list] If you wish to participate in the open beta, you can find this version in the public_beta branch on Steam. The way to access it is as follows: Steam client → LIBRARY → right click on Euro Truck Simulator 2 → Properties → Betas tab → public_beta → 1.28 public beta. No password required. During open betas, there is a dedicated beta site for World of Trucks, which is used to safely test all new features.

我希望有人可以帮助我, 干杯'

【问题讨论】:

  • 您能否说明您要发送给bb内容
  • 没有解决问题,但是这个'#\[\*\\](.*)\[\/\*\\]#iUs' 只匹配这个[*\]asdf[/*\] .. 它应该是这个'#\[\*\](.*)\[/\*\]#iUs' 另外,使用.* 将匹配到行尾,甚至其他标签。
  • 您的示例中有两种不同的 bbcode 项目语法:第一个仅使用 [*] 开始新项目,第二个使用结束标记结束项目。哪一个是正确的? (我已经看过第一个但从未见过第二个)
  • 而且,由于您有 dot-all 修饰符,.* 实际上会直接跳到字符串的 end 并回溯以找到结束标记。
  • 无论如何,您可以构建一个模式来处理第一个语法(没有结束标记)并删除最终的结束标记(如果有)。

标签: php regex preg-replace bbcode


【解决方案1】:

有几种不同语法的 bbcode 风格。显然最好的办法是有一个明确的规则并且只处理一种语法,但是对于您的具体问题,您可以将您的模式更改为这样的:

#\[\*]([^[]*(?:\[(?!/?\*]|/list])[^[]*)*)(?:\[/\*])?#i

demo

请注意,您还需要将 [*] 替换放在 [list]s 替换之前。

这个想法是描述所有不是[*][/*][/list],并在末尾添加一个可选的结束标记。

详情:

\[\*]   # opening tag
 (    # capture group 1
     [^[]* # all that isn't an opening square bracket
     (?:
        \[ (?!/?\*]|/list]) # opening bracket not followed by *] or /*] or /list]
         [^[]*
     )*
 )
 (?:\[/\*])?    # the optional closing tag

【讨论】:

  • 说实话 - 我不懂语法。如果我用你的替换我的“star”-syntax,输出看起来会很好,但是我会得到太多的结束标签和丢失的开始标签,比如这里:(缺少开始标签)Auxiliary brakes system support (engine brake and retarder in one control element)[/*](并添加结束标签 - 然后更正开始标签)• Fixed licence plate change in states/countries where no city formats exist[/*](添加了太多结束标签)prntscr.com/g1y5wh //编辑:哇快速编辑解释 - 谢谢!
  • @Kaan2106:对不起,我打错了,(我忘记了结束标签中的斜线。)
  • @Kaan2106:您还需要将项目替换放在数组中的列表替换之前。 (因为模式指的是[/list] bbcode)
猜你喜欢
  • 2021-09-29
  • 2022-01-03
  • 2011-10-10
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 2011-03-14
相关资源
最近更新 更多