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?
The preg_match() function returns whether a match was found in a string.
The preg_match() function will tell you whether a string contains matches of a pattern.
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 .
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.
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", ....)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With