开发者

How can i know some characters is in the string or not?

开发者 https://www.devze.com 2023-02-09 01:24 出处:网络
How can i know some charac开发者_开发技巧ters are in the string or not? i have 5 invalid characters for example that can not use in the username.

How can i know some charac开发者_开发技巧ters are in the string or not? i have 5 invalid characters for example that can not use in the username. {-()*#} how can i do this?


If you just want to check if every single character is a letter or a number you coud use:

if(ctype_alnum($text)){
   echo "Success!";
}


if (preg_match('/[-()*#]/', $string)) {
    echo 'string contains one or more illegal characters';
}

if (preg_match('/[^a-zA-Z0-9]/', $string)) {
    echo 'string contains non-alphanumeric characters';
}


$disallowed = array('-', '(', ')', '*', '#');
$valid = true;
foreach ($disallowed as $char) {
    if (strpos($username, $char) !== false) $valid = false;
}

if ($valid)
  echo 'valid username';
else 
  echo 'invalid username';


   if (preg_match('/(\{|\(|-|\)|\*|#|\})+/g', $string)) {
      echo 'invalid characters';
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消