简体   繁体   English

PHP验证字符串不包含值列表

[英]PHP Validation String Does Not Contain List Of Values

for example I might: 例如,我可能会:

$exclude_values = ['/','.'];
$check_string = 'asdf/';
$return = 'VALID';
foreach($exclude_values as $value)
{
  if(strpos($value,$check_string) != FALSE)
  {
   $return = 'INVALID';
  }
}

return $return;

Is there a better way to do this? 有一个更好的方法吗? I've seen examples of single checks on stack just not multiple values 我看过堆栈中的单个检查的示例,但没有多个值

I can't recommend any string searching function outside the multibyte or PCRE lib, because they are not always utf-8 compatible, and you'll have strange errors sooner or later. 我不建议在多字节PCRE库之外使用任何字符串搜索功能,因为它们并不总是与utf-8兼容,而且迟早会出现奇怪的错误。

In your case a regex is a much better solution: 在您的情况下,正则表达式是更好的解决方案:

return !preg_match('%[/\.]%usD', $check_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM