ereg 到 preg 问题

发布于 2024-11-17 12:49:20 字数 1732 浏览 2 评论 0原文

我正在使用下载的 PHP 脚本来加快网站的开发速度,但我收到了一堆与 ereg 相关的“已弃用”消息。我将在上线前关闭警告,但我会尽力确保首先清除所有警告!

我找到了便捷指南 这对于简单的位来说是有好处的,例如使用 / 和 /i 将 eregi_replace 更改为 preg_replace 但我遇到了一些更高级的表达式(带有注释、串联、变量、大括号)的问题还有更多 - 有些是相同的类型,但我在尝试和打破它时感到非常困惑!)。

谁能告诉我在更新以下内容时应该将 / 或 / /i 放在哪里,或者,同样欢迎,告诉我如何处理每种类型(或向我指出说明),以便我可以自己做?

eregi("<!--\ startBlock\(([^)]+)\)\ -->", $content["body"], $m)

eregi($reg, $content["body"], $m)

eregi("([^\.]+)\.(.*)", $variable, $m)  ... I think (?) this is preg_replace("/([^\.]+)\.(.*)/i", $variable, $m)

eregi("{url:([^}]+)}", $txt, $m))

eregi_replace("{".$key."}", $val, $txt)

eregi_replace("{script}", $HTTP_SERVER_VARS["SCRIPT_NAME"], $txt)

eregi_replace("{ifNotSet:".$m[1].":([^}]+)}", "", $txt)

更新: 我似乎一切正常(感谢您的帮助),除了下面的一个功能,我无法弄清楚 preg 版本 - 我尝试了各种组合,但结果页面从未给出与 eregi 版本相同的结果(使用错误行)。有什么建议吗?

function parse(&$content) {
while (preg_match("/<!--\ startBlock\(([^)]+)\)\ -->/i", $content["body"], $m)) {
  $name = $m[1];
  $block = array();
  $block["name"] = $name;
  $block["blocks"] = array();
  $block["used"] = 0;
  $block["values"] = array();
  $reg = "<!--\ startBlock\(".$name."\)\ -->(.*)<!--\ endBlock\(".$name."\)\ -->";
  if (!eregi("$reg", $content["body"], $m)) {
$this->error("block `".$name."' does not have startBlock() AND endBlock().");
  }
  $block["body"] = $m[1];
  $content["body"] = eregi_replace("$reg", "{".$name."}", $content["body"]);
  $content["blocks"][$name] = array();
  $this->parse(&$block);
  $content["blocks"][$name][0] = $block;
}
}

然后我将开始“通话时间传递参考”,这看起来更棘手!

I'm using a downloaded PHP script to speed up development of my site, but I'm having a stack of 'deprecated' messages coming up relating to ereg. I'm going to turn off warnings before going live, but am trying to make sure I clear them all first!

I found a handy guide which is good for the simple bits, such as changing eregi_replace for preg_replace with / and /i but I'm running into problems with some of the more advanced expressions (with comments, concatenation, variables, braces and more - some are the same types, but I'm getting so confused trying and breaking it!).

Can anyone tell me where I should put the / or / /i when I update the following, or, just as welcome, tell me what do do with each type (or point me at instructions) so I can do it myself?

eregi("<!--\ startBlock\(([^)]+)\)\ -->", $content["body"], $m)

eregi($reg, $content["body"], $m)

eregi("([^\.]+)\.(.*)", $variable, $m)  ... I think (?) this is preg_replace("/([^\.]+)\.(.*)/i", $variable, $m)

eregi("{url:([^}]+)}", $txt, $m))

eregi_replace("{".$key."}", $val, $txt)

eregi_replace("{script}", $HTTP_SERVER_VARS["SCRIPT_NAME"], $txt)

eregi_replace("{ifNotSet:".$m[1].":([^}]+)}", "", $txt)

Update:
I seem to have it all working (thanks for the help) except one function below, where I can't figure out the preg versions - I have tried various combinations but the resulting page never gives the same result as the eregi version (with the error line). Any suggestions?

function parse(&$content) {
while (preg_match("/<!--\ startBlock\(([^)]+)\)\ -->/i", $content["body"], $m)) {
  $name = $m[1];
  $block = array();
  $block["name"] = $name;
  $block["blocks"] = array();
  $block["used"] = 0;
  $block["values"] = array();
  $reg = "<!--\ startBlock\(".$name."\)\ -->(.*)<!--\ endBlock\(".$name."\)\ -->";
  if (!eregi("$reg", $content["body"], $m)) {
$this->error("block `".$name."' does not have startBlock() AND endBlock().");
  }
  $block["body"] = $m[1];
  $content["body"] = eregi_replace("$reg", "{".$name."}", $content["body"]);
  $content["blocks"][$name] = array();
  $this->parse(&$block);
  $content["blocks"][$name][0] = $block;
}
}

and then I'll get started on the "call-time pass-by references", which seem even trickier!

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

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

发布评论

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

评论(2

挽心 2024-11-24 12:49:20

我假设 / 你指的是模式开始和结束的分隔符?将它们想象成围绕字符串的双引号。它们需要位于模式的开头和结尾,并且模式中的任何 / 都需要转义 \/,您可以在末尾放置修饰符,例如 < code>i,表示它将检查模式,不区分大小写。所以 /pattern/i 将检查模式(不区分大小写),并将 翻译为 preg,它会类似于 / <\/a>/ (如果需要,您可以在后面添加 i)。

您可以将反斜杠设置为其他内容(例如 @ 符号或其他内容),但现在,我只想使用它们,以使事情变得简单。可能还需要进行其他修改,但这似乎是最大的修改。

I assume by the / you mean the delimiters to start and end the pattern? Think of them like the double quotes the surround the string. They need to be at the beginning and end of the pattern, and any / in the pattern need to be escaped \/, you can put modifiers at the end, like i, which states it will check the pattern case-insensitive. so /pattern/i will check for pattern (case-insensitive), and to translate say </a> to preg, it would be something like /<\/a>/ (and you can add i after if you want).

You can make the backslashes something else (say @ signs, or something), but for now, I would just use them, to keep things simple. There may be other modifications needed, but that seems to be the big one.

戏舞 2024-11-24 12:49:20

// 只是边框字符,您实际上可以使用几乎任何您想要的字符,只要它们不会出现在正则表达式的其他位置即可。 /i 表示不区分大小写,这意味着模式将计算大写或小写。

要记住的重要一点是,如果您将 \ 放在某个字符前面,它将对其进行转义,而不是将其视为特殊字符。

The / / are just border characters, you can actually use almost any one you want, as long as they they do not occur elsewhere in the regex. /i means case-insensitive, meaning the patterns will math either upper or lower case.

The important thing to remember, is if you put \ in front of a character it will escape it, and not treat it as the special character it is.

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