preg_replace 对于专业人士来说很容易

发布于 2024-08-04 16:14:40 字数 194 浏览 2 评论 0原文

我想找到第一个 select...from 并仅替换它,以下代码替换 sql 查询中的所有 select..from,我只需要第一个 select..from

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);

i want to find first select ... from and replace that only, following code replace all select..from in sql query, i just need for first select..from

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql);

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

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

发布评论

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

评论(1

神妖 2024-08-11 16:14:40

preg_replace() 定义替换数量,默认为 -1(意味着无限制

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql, 1);

There is a fourth optional parameter called 'limit' for preg_replace() which defines the number of replacements, and which defaults to -1 (meaning no limit)

preg_replace('#select(.*?)from#is', "select count($expr) as counted from", $sql, 1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文