2012-11-01 72 views
0

我不想让在字符串中的多个模式,比如,检查多个和组合模式与PHP的preg_match

$string = "php in stackoverflow"; // multiple spaces not allowed 
$string = "php in--stackoverflow"; // multiple hyphens not allowed 
$string = "php in__stackoverflow"; // multiple underscores not allowed 
etc 

所以,它的工作原理与这些线,

if(preg_match('/\s\s+/', $string)) echo "only a single spaces are allowed"; 
if(preg_match('/\-\-+/', $string)) echo "only a single hyphens are allowed"; 
if(preg_match('/\_\_+/', $string)) echo "only a single underscores are allowed"; 

我也不想让下面的组合模式及以上线路不能正常工作,

$string = "php in -stackoverflow"; // a space with a hyphen not allowed 
$string = "php in-_stackoverflow"; // a hyphens with a underscore not allowed 
$string = "php in_ stackoverflow"; // a underscore with a space not allowed 
etc 

任何想法,我能达到THI用一个简单的脚本?

+0

你想检查连续两个这些字符吗? – dpk2442

+0

如果可能的话...... – laukok

回答

2

这是字符类用于匹配多个字符。

if(preg_match('/[\s-_][\s-_]+/', $string)) echo "only a single space, hyphen or underscore is allowed"; 
+0

谢谢你的帮助,Jon。这是完美的! :) – laukok