简体   繁体   中英

php check if string contains some letters/numbers

I have some string in which I would like to check if contains some letters or numbers. Those letters are from az, AZ and all numbers.

So this is the example: $goodstring = "abcdefg%$#%&asdqwe"; $badstring = "%$#&%#/&/#$%!#-.";

check if $goodstring contains letters/numbers and if, then return true. check if $badstring contains letters/numbers and if not, then return false.

I have also used preg_match, but this works only if there are not some letters/numbers in string.

Here you go (" only " version):

$containsOnlyLettersOrNumbers = (preg_match('~^[0-9a-z]+$~i', $string) > 0);

Or (depending on what you want exactly):

$containsLettersOrNumbers = (preg_match('~[0-9a-z]~i', $string) > 0);
var_dump(preg_match('#[0-9]|[a-z]#i', $goodstring));
var_dump(preg_match('#[0-9]|[a-z]#i', $badstring));

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