Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - "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:xampphtdocsswebookincludesclasses.php on line 22

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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", ....)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...