用数组解析 bbcode
Parse bbcode with arrays
以下问题:
上面 "list" 中的 "li"-tag 设置不正确,然后是。
我尝试更改表达式,但我还没有找到解决方案。
这是我的(缩短的)代码(以及所有其他 "bb" 都遵循相同的原理图)
function bb($content) {
$search = array (
'#\[list\](.+)\[\/list\]#iUs',
'#\[list=1\](.+)\[\/list\]#iUs',
'#\[\*\](.*)\[/\*\]#iUs',
'#\[IMG\](.+)\[\/IMG\]#iUs'
);
$replace = array (
'<ul></ul>',
'<ol></ol>',
'<li></li>',
'<img src=""/>'
);
$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.
我希望有人能帮助我,
干杯'
有几种语法不同的 bbcode 风格。显然最好的是有一个明确的规则并且只处理一种语法,但是对于您的特定问题,您可以将您的模式更改为如下所示:
#\[\*]([^[]*(?:\[(?!/?\*]|/list])[^[]*)*)(?:\[/\*])?#i
请注意,您还需要将 [*]
替换放在 [list]
替换之前。
想法是描述所有不是 [*]
、[/*]
或 [/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
以下问题:
这是我的(缩短的)代码(以及所有其他 "bb" 都遵循相同的原理图)
function bb($content) {
$search = array (
'#\[list\](.+)\[\/list\]#iUs',
'#\[list=1\](.+)\[\/list\]#iUs',
'#\[\*\](.*)\[/\*\]#iUs',
'#\[IMG\](.+)\[\/IMG\]#iUs'
);
$replace = array (
'<ul></ul>',
'<ol></ol>',
'<li></li>',
'<img src=""/>'
);
$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.
我希望有人能帮助我, 干杯'
有几种语法不同的 bbcode 风格。显然最好的是有一个明确的规则并且只处理一种语法,但是对于您的特定问题,您可以将您的模式更改为如下所示:
#\[\*]([^[]*(?:\[(?!/?\*]|/list])[^[]*)*)(?:\[/\*])?#i
请注意,您还需要将 [*]
替换放在 [list]
替换之前。
想法是描述所有不是 [*]
、[/*]
或 [/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