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
}

对于以下情况,这将匹配 true:

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