只保留连续重复的指定字符的一个实例

发布于 2024-11-29 21:48:57 字数 229 浏览 5 评论 0原文

我有一个字符串 !!test!!!!!测试。

我想要做的是使用 preg_replace() 并仅保留第一个 ! 但保留第一个 ! 之后的其余文本> 这不是 !

!!test -> !测试

I have a string !!test or !!!!! testing.

What I want to be able to do is use preg_replace() and only keep the first ! but keep the rest of the text after that first ! that is not a !

!!test -> !test

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

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

发布评论

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

评论(2

挽容 2024-12-06 21:48:57
$str = preg_replace('/!+/', '!', $str);

将多个 ! 替换为单个 !

http://www.regular-expressions.info/ 有一些很棒的教程和参考资料用于学习正则表达式。

$str = preg_replace('/!+/', '!', $str);

Replace multiple !s with a single !.

http://www.regular-expressions.info/ has some great tutorials and references for learning regular expressions.

烟酒忠诚 2024-12-06 21:48:57
preg_replace('#!+\s?test#','!test', $subject);

适用于两个示例。

preg_replace('#!+\s?test#','!test', $subject);

Works for both examples.

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