BBCode 需要 preg_replace 帮助

发布于 2024-10-15 12:20:12 字数 1193 浏览 2 评论 0原文

我不太清楚与 PHP 的 preg_replace 函数一起使用的正确模式。这是 phpBB 论坛上的一些 BBCode 的示例。

[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]

我想删除可能嵌入文本中的任意数量的开始和结束字体标签,但将文本保留在中间。在这种情况下,我想最终得到:

[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]

字体的名称各不相同,有些名称中包含空格,有些则没有。无论如何,模式匹配都必须有效。

提前致谢!

I can't quite figure out the right pattern to use with PHP's preg_replace function. Here is an example of some BBCode on a phpBB forum.

[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]

I'd like to remove the opening and closing font tags, any number of them, that might be embedded in the text, but keep the text in between. In this case, I would like to end up with:

[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]

The name of the fonts vary, some have spaces in the names, some don't. The pattern match would have to work regardless.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

﹎☆浅夏丿初晴 2024-10-22 12:20:12

如果您只是删除它们,则不需要太复杂的正则表达式...

$str = '[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]';

$regex = '/\[font=.*?\]|\[\/font\]/i';

$str = preg_replace($regex, '', $str);

var_dump($str);

输出

string(457) "[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We donÕt want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]"

在 ideone 上查看

If you are simply removing them, you don't need a too complicated regex...

$str = '[color=black][font=Times New Roman][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We don’t want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/font][/color]';

$regex = '/\[font=.*?\]|\[\/font\]/i';

$str = preg_replace($regex, '', $str);

var_dump($str);

Outputs

string(457) "[color=black][color=black]Thanks. As discussed last night/this morning, we were able to Cable and provide some small relief. After enough waste water was evacuated, the camera diagnosis found at 30 foot from clean-out, heavy root penetration and possible damage/separation to the line. We donÕt want to throw a cost to fix/repair without a second opinion from xxx. We will have solid cost of repair later today of first thin in the morning. [/color][/color]"

See it on ideone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文