Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace: bad regex == 'Unknown Modifier'?

I'm making up fake email addresses and I just want to make sure they are in a valid email format so I'm trying to remove any character that is not in the set below:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/g', '', $jusr['email']);

I haven't had any trouble on my windows machine, but on the linux dev server I get this error each time this code runs:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'g' in /var/www/vhosts/....

I think it's the regex string, but I can't pin it down. Little help? Thanks.

Clarification: I'm not trying to accommodate all valid email addresses (unnecessary for my purpose), I just need to figure out what's wrong with my preg_replace regex.

like image 535
doub1ejack Avatar asked Oct 04 '11 19:10

doub1ejack


1 Answers

g is not a valid modifier in PCRE (the regex implementation PHP uses) because it's simply not needed; preg_replace() will perform global replacements by default. You'll find the modifier in true Perl regex as well as JavaScript regex, but not in PCRE.

Just drop the g:

$jusr['email'] = preg_replace('/[^a-zA-Z0-9.-_@]/', '', $jusr['email']);
like image 129
BoltClock Avatar answered Sep 17 '22 15:09

BoltClock