00 Votes

PHP: preg_match - Unknown Modifier Error although using correct Regular Expression

Question by Guest | 2014-03-12 at 18:20

Although I am using a correct regular expression in my PHP script, I am always getting the following error:

Warning: preg_match() [function.preg-match]: Unknown modifier '@'

I am using the following call of the function:

preg_match("^[^@]+@[^@]+$", $str)

Unfortunately, I cannot even imagine where the error is hidden in this line! In all of the other programs, I have tried this RegEx, it is working like a charm! Can someone help me?

ReplyPositiveNegative
1Best Answer1 Vote

You have to surround your regular expression with a delimiter. Possible delimiters are  "/", "~" or "#", for example.

It should work this way:

preg_match("/^[^@]+@[^@]+$/", $str)
preg_match("~^[^@]+@[^@]+$~", $str)
preg_match("#^[^@]+@[^@]+$#", $str)

In principle, you can use each character as the delimiter that is not alphanumeric. Also a backslash or whitespace is not allowed.

Within your regular expression, you used delimiter has to be escaped. That is why, you should preferably use a delimiter that is not occurring within your regular expression.

Modifier for your regular expression can be written behind the delimiter:

preg_match("/^[^@]+@[^@]+$/i", $str)

More information you can get on the official PHP page.
2014-03-13 at 21:31

ReplyPositive Negative
Reply

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.