2012-11-01 96 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