开发者

How to convert eregi to preg_match?

开发者 https://www.devze.com 2022-12-23 17:30 出处:网络
I am using a lib which uses eregi($match=\"^http/[0-9]+\\\\.[0-9]+[ \\t]+([0-9]+)[ \\t]*(.*)\\$\",$line,$matches)

I am using a lib which uses

eregi($match="^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$",$line,$matches)

but as eregi is deprecated now,开发者_开发技巧 i want to convert above to preg_match. I tried it as below

preg_match($match="/^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i",$line,$matches)

but it throws an error saying Unknown modifier '[' in filename.php

any ideas how to resolve this issue?

Thanks


If you use / as the regex delimiter (ie. preg_match('/.../i', ...)), you need to escape any instances of / in your pattern or php will think it's referring to the end of the pattern.

You can also use a different character such as % as your delimiter:

preg_match('%^http/[0-9]+\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)$%i',$line,$matches)


You need to escape the delimiters inside the regular expression (in this case the /):

"/^http\\/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$/i"

But you could also chose a different delimiter like ~:

"~^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$~i"


You can try:

preg_match("@^http/[0-9]+\\.[0-9]+[ \t]+([0-9]+)[ \t]*(.*)\$@i",$line,$matches)
  • You can drop the the $match=
  • You are using / as the delimiter and there is another / present in the regex after http, which effectively marks the end of your regex. When PHP sees the [ after this it complains.
  • You can use a different set of delimiters as @ or escape the / after http
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号