0

文字列に複数のパターンを許可したくない、たとえば、

$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

簡単なスクリプトでこれを達成できるアイデアはありますか?

4

1 に答える 1

2

これは、複数の文字に一致する文字クラスの目的です。

if(preg_match('/[\s-_][\s-_]+/', $string)) echo "only a single space, hyphen or underscore is allowed";
于 2012-11-01T17:48:31.847 に答える