Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unknown modifier 'g' in..." when using preg_match in PHP?

This is the regex I'm trying to use:

/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim 

I found it on this site, and it works great when I try it out there. But as soon as I place it in my code, I get this message:

Warning: preg_match() [function.preg-match]: Unknown modifier 'g' in C:\xampp\htdocs\swebook\includes\classes.php on line 22 

Can anyone explain what's wrong, and why it's working on that website and not in my code?

like image 895
Nike Avatar asked Aug 26 '10 19:08

Nike


People also ask

What is the use of Preg_match () method?

The preg_match() function returns whether a match was found in a string.

What is the purpose of Preg_match () regular expression in PHP?

The preg_match() function will tell you whether a string contains matches of a pattern.

What value is return by Preg_match?

Return Values ¶ preg_match() returns 1 if the pattern matches given subject , 0 if it does not, or false on failure. This function may return Boolean false , but may also return a non-Boolean value which evaluates to false .

What is Preg_match_all in PHP?

PHP preg_match_all() Function The preg_match_all() function returns the number of matches of a pattern that were found in a string and populates a variable with the matches that were found.


1 Answers

There is no modifier g for preg_match. Instead, you have to use the preg_match_all function.

So instead of:

preg_match("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/gim", ....) 

use:

preg_match_all("/^(\w|\.|-)+?@(\w|-)+?\.\w{2,4}($|\.\w{2,4})$/im", ....) 
like image 185
codaddict Avatar answered Sep 18 '22 09:09

codaddict