0

有効なエントリが 's' 'a' 't' または 'b' または文字列 'all' である文字列があります。エントリーがいつ無効になるかを知りたい。悪い文字列をテストするには、否定が必要なようです。結果は次のようになります。

preg_match('/[^satb(all)]/', 's')  ==> should be false (and is)
preg_match('/[^satb(all)]/', 'sall')  ==> should be false (and is)
preg_match('/[^satb(all)]/', 'alsl')  ==> should be true but is not (the l's are not part of 'all')

いろいろな組み合わせを試しましたが、うまくいきません。助けてくれてありがとう。

4

1 に答える 1

0

文字クラスは単一文字のみに一致します。私はあなたが欲しいと思うこれは:

if ( preg_match('/^(all|[satb]+)$/', $string) ) {
  // string is valid, do something
} else {
  // string is invalid, do something
}

これは、次の場合に当てはまります。

$string = "all";
$string = "s";
$string = "a";
$string = "t";
$string = "b";
于 2013-11-11T20:44:11.110 に答える