'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??
Solution 1:[1]
If you want to exclude letters, you can simply search for [a-z] 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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
