Nida Zubair
Nida Zubair

Reputation: 23

Match multiple delimiters containg backslashes php

I have a php code that needs to be matched for any of the following string using preg_match using this code

if(preg_match('/(image/gif)|(image/jpg)|(image/jpeg)/',$between))
{
    echo "Match Found";
}

else

echo "Match Not Found";

but i get this error

Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\xampp\htdocs\project\extension.php on line 38

Any help will be appreciated....I googled alot but couldn't find solution...

Upvotes: 1

Views: 129

Answers (3)

zerkms
zerkms

Reputation: 254926

As long as you want / to be used inside regular expression - use ~ as a regex delimiter instead:

if(preg_match('~(image/gif)|(image/jpg)|(image/jpeg)~',$between))
               ^-----------                         ^--------

or even better:

if(preg_match('~image/(gif|jpe?g)~',$between))

Upvotes: 0

Jeroen
Jeroen

Reputation: 13257

Replace your preg_match pattern with this:

'/(image\/gif)|(image\/jpg)|(image\/jpeg)/'

You should always escape characters like /

Upvotes: 1

Jon
Jon

Reputation: 437386

You are using / as the delimiter character, so when it appears inside your regex you must escape it:

if(preg_match('/(image\/gif)|(image\/jpg)|(image\/jpeg)/',$between))

Alternatively, you can choose another delimiter:

if(preg_match('~(image/gif)|(image/jpg)|(image/jpeg)~',$between))

Upvotes: 1

Related Questions