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)){