关于 eregi() 和 preg_match() 的问题
我的代码是
if(eregi($pattern,$file)){ $out['文件'][]=$文件; }else
但这在 php 5.3 中不起作用,它显示警报
Function eregi() is deprecated
所以我更改为
if(preg_match($pattern,$file) ){ $out['文件'][]=$文件; }else
但现在它显示
preg_match(): Noending delimiter '.'发现
我是否输入了错误的语法?
My code was
if(eregi($pattern,$file)){
$out['file'][]=$file;
}else
But is doesn't work in php 5.3, it shows the alert
Function eregi() is deprecated
so I changed to
if(preg_match($pattern,$file)){
$out['file'][]=$file;
}else
But now it shows
preg_match(): No ending delimiter '.' found
Did I enter any wrong syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该模式需要有某种 分隔符 围绕它。
/
是典型的,但可以使用“任何非字母数字、非反斜杠、非空白字符”。只需确保您的分隔符不会出现在$pattern
本身中。因此,如果您的模式是
http://(.*)
,其中已经包含/
字符,您可能需要选择其他内容,例如~:
或者,如下面的 @jensgram 注释,如果您不能保证您的模式不会包含特定的分隔符字符,您可以使用 preg_quote(),就像这样:
哦,还有,因为你使用的是
eregi()
(不区分大小写),您需要添加i
修饰符 用于在分隔符之外对模式不区分大小写。The pattern needs to have some sort of delimiter character surrounding it.
/
is typical, but "any non-alphanumeric, non-backslash, non-whitespace character" could be used. Just make sure your delimiter character doesn't appear in the$pattern
itself.So if your pattern was
http://(.*)
, which already has/
characters in it, you might want to choose something else like~
:Alternatively, as @jensgram notes below, if you can't guarantee your pattern won't contain a certain delimiter character, you could escape those characters in the pattern with preg_quote(), like so:
Oh, also, since you're using
eregi()
(case-insensitive), you'll want to add thei
modifier for case-insensitive to your pattern, outside the delimiter.