2014-09-26 45 views
-1

如果用户在文本输入中输入了id为“my_input”的文字“sun”,“moon”或“stars”,则该脚本执行某些操作。preg_match的通配符

我的脚本应该如何查看才能使用通配符?例如:如果用户输入 “星星是光明的”,剧本仍然没有的东西...

if(preg_match("/^(sun|moon|stars)$/i", $_POST['my_input']) === 1) { 
    // do stuff 
} 

回答

1

通配符正则表达式是.*

if(preg_match("/.*(sun|moon|stars).*$/i", $_POST['my_input']) === 1) { 
    // do stuff 
} 

这里测试phpliveregex.com

+0

谢谢!但是现在脚本执行“// do stuuf”,即使在用户输入“The starsssssss are bright”...我需要完全匹配“stars”... – Malasorte 2014-09-26 12:14:06

+0

好的。我应该在\ b之前和之后加上\ b ... 谢谢! – Malasorte 2014-09-26 12:24:22