Reputation: 123
How might you use preg_match
to detect any text characters in a string?
Upvotes: 0
Views: 2654
Reputation: 455470
You can do:
if(preg_match('/[a-z]/i',$input)) {
echo $input,' has a text character';
}
The regex used: [a-z]
is a character class which matches the lowercase characters.
We also use the i
modifier to make the match case insensitive effectively making [a-z]
match both the lowercase and uppercase characters.
Upvotes: 3
Reputation: 2769
Just to detect if they're there?
preg_match('/[A-Za-z]/', $subject, $matches_arr);
That should do the trick.
Upvotes: 1
Reputation: 151264
you can use
preg_match('/[a-z]/i', 'hi test string');
for example
var_dump(preg_match('/[a-z]/i', 'hi')); # gives 1
var_dump(preg_match('/[a-z]/i', '1')); # gives 0 as false
Upvotes: 1