简体   繁体   中英

PHP Regular Expression to check Alphameric String

I wrote the PHP code using Regular Expression . I give the following array as input string:

$alphmericGoodCases = array('ASDFGH123', 'ASFGH1234', '1234567', '555 abcdf', '#$&%^@-');
foreach($alphmericGoodCases as $alphmericGoodCase){
if(preg_match('/^[A-Za-z0-9\s_-]+$/', $alphmericGoodCase)) {
   echo "true";
}
 else 
   echo "false";
}

The output is like as true , true , true , true and false. But, I want the last one is also true. Where I am missing the Regular Expression ?

If you want the special characters, just use this shorter version:

$alphmericGoodCases = array('ASDFGH123', 'ASFGH1234', '1234567', '555 

abcdf', '#$&%^@-');
foreach($alphmericGoodCases as $alphmericGoodCase)
{

   if(preg_match('/^.+$/', $alphmericGoodCase)) {
      echo "true";
   }
   else 
      echo "false";
}

This basically says "match any string, except the newline character"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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