简体   繁体   中英

Regex function to check the presence of any letter on the string or only number or symbols

I'm a little confused about the usage of regular expression in all the entry string.

I need to understand if a given string contain any number or symbols, or viceversa don't contain any letters:

good no good
!
- 1 -
=>
foo
foo bar
{
I have 3 bananas
123 456

So I need something like that /[^A-Za-z]/ but in the strign I have 3 bananas the preg_math_full is fired, how can I use the regex pattern in all the string??

If you want to exclude letters, you can simply search for [az] and reverse the result, such as:

$testArray = [
    "!",
    "- 1 -",
    "=>",
    "foo",
    "foo bar",
    "{",
    "I have 3 bananas",
    "123 456"
];

foreach ($testArray as $element)
{
    echo "$element is ";
    //  v--- check this
    if (!preg_match("#[a-z]#i", $element))
    {
        echo "good" . PHP_EOL;
    }
    else
    {
        echo "not good" . PHP_EOL;
    }
}

This will output:

! is good
- 1 - is good
=> is good
foo is not good
foo bar is not good
{ is good
I have 3 bananas is not good
123 456 is good

Fiddle

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