PHP - 用 preg 替换 ereg

发布于 2024-08-05 18:32:48 字数 132 浏览 4 评论 0原文

我正在尝试从网站中删除一些已弃用的代码。 谁能告诉我相当于

ereg_replace("<b>","<strong>",$content);

谢谢的preg。

I'm trying to remove some deprecated code from a site.
Can anyone tell me the preg equivalent of

ereg_replace("<b>","<strong>",$content);

Thanks.

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

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

发布评论

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

评论(2

污味仙女 2024-08-12 18:32:48

似乎根本不需要正则表达式。

一个简单的 str_replace 就可以了:

$cleaned = str_replace  ('<b>', '<strong>', $unCleaned);

如果您需要更复杂的替换,例如检查属性,你可以这样做:

$cleaned = preg_replace('/<b(\s[^>]*)?>/', '<strong\\1>', $unCleaned);

但这绝不是完美的;像

这样的东西会破坏正则表达式。

There seems to be no need for regular expressions at all.

a simple str_replace would do:

$cleaned = str_replace  ('<b>', '<strong>', $unCleaned);

If you need more complicated replacements, for example checking the attributes, you could do:

$cleaned = preg_replace('/<b(\s[^>]*)?>/', '<strong\\1>', $unCleaned);

But this is by no means perfect; something like <div title="foo->bar"></div> would break the regular expression.

十秒萌定你 2024-08-12 18:32:48

与 ERE 正则表达式等效的 PCRE 是:

preg_match("/<b>/", "<strong>", $content)

但正如 Jacco 已经指出的那样,您根本不需要正则表达式,因为您想要替换常量值。

A PCRE equivalent to your ERE regular expression would be:

preg_match("/<b>/", "<strong>", $content)

But as Jacco already noted you don’t need a regular expression at all as you want to replace a constant value.

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