【问题标题】:side effects of using ereg_replace and its replacement使用 ereg_replace 及其替代品的副作用
【发布时间】:2012-07-11 07:06:38
【问题描述】:

我是 php 新手。我有以下代码:

$this->image["format"] = ereg_replace(".*\.(.*)$", "\\1", $imgfile);
$this->image["outputformat"] = ereg_replace(".*\.(.*)$", "\\1", $save);

工作正常,但我收到错误已弃用:函数 ereg_replace()。 我想问一下在php中使用不推荐使用的函数是否有任何副作用?如果有其他替代品。我试过 preg 但它也不起作用。在此先感谢:)

【问题讨论】:

  • 您也可以使用$ext = array_pop(explode('.', $file_name))提取文件扩展名
  • 但我想将其转换为 preg_replace 能否请您帮我处理替换后的结果代码。
  • 这可能只是在您的正则表达式中添加分隔符的问题,其他一切看起来都很好。你试过preg_replace('/.*\.(.*)$/', '\\1', $imgfile) 吗?

标签: php


【解决方案1】:

使用不推荐使用的函数是不好的做法,您可以使用它们,但不建议这样做,因为它们不再受支持并且可能不会出现在更高版本的 php 中。

您应该使用preg_replace 而不是您的函数(我认为您已经尝试过,但请查看文档,也许您做错了什么)

编辑在 cmets 中回答问题:

您的替换将如下所示:

$this->image["format"] = preg_replace("/.*\.(.*)$/", "\\1", $imgfile);
$this->image["outputformat"] = preg_replace("/.*\.(.*)$/", "\\1", $save);

请注意,模式以正斜杠开头和结尾/.*\.(.*)$/

【讨论】:

  • 能否请您告诉我在将其转换为 preg_replace 后的替代品可能是我做的不正确。
猜你喜欢
  • 1970-01-01
  • 2021-05-11
  • 1970-01-01
  • 1970-01-01
  • 2019-05-28
  • 2018-05-04
  • 1970-01-01
  • 2011-10-15
  • 2011-04-08
相关资源
最近更新 更多