2

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?

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
Juni
  • 307
  • 2
  • 5
  • 10
  • The error message `Function eregi() is deprecated` is a warning. It does not mean what you think it means. The function is **still working**. It says that it **might not** work in future PHP versions. (Unlikely) – mario Aug 30 '11 at 16:24
  • @mario I believe it's expected to be removed in PHP6, though who knows when that will be. :-) – Wiseguy Aug 30 '11 at 16:27
  • @Wiseguy: Since the plans for the mythical PHP6 were retired, so were the musings of actual deprecation. There's no roadmap, so this is speculation. (And not an overly probable one anyway.) – mario Aug 30 '11 at 16:34
  • @mario Thanks. It's often hard to find what is or is not official. – Wiseguy Aug 30 '11 at 16:37

1 Answers1

3

The pattern needs to have some sort of delimiter character surrounding it.

if(preg_match('/' . $pattern . '/',$file)){

/ 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 ~:

if(preg_match('~' . $pattern . '~',$file)){

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:

if(preg_match('~' . preg_quote($pattern, '~') . '~',$file)){

Oh, also, since you're using eregi() (case-insensitive), you'll want to add the i modifier for case-insensitive to your pattern, outside the delimiter.

if(preg_match('~' . $pattern . '~i',$file)){
Community
  • 1
  • 1
Wiseguy
  • 20,522
  • 8
  • 65
  • 81
  • 1
    Consider [`preg_quote()`](http://www.php.net/manual/en/function.preg-quote.php): `preg_match('~' . preg_quote($pattern, '~') . '~i', $file)`. It may prove useful should you ever add the delimiter to the `$pattern`. – jensgram Aug 30 '11 at 16:16
  • Indeed, I thought about mentioning that. I'll add it. – Wiseguy Aug 30 '11 at 16:17
  • Of course, if you are using `preg_quote()` on the whole pattern, you could replace it all with `strpos()` :) – Álvaro González Aug 30 '11 at 16:20