Parker
Parker

Reputation: 123

Preg_Match Syntax

How might you use preg_match to detect any text characters in a string?

Upvotes: 0

Views: 2654

Answers (3)

codaddict
codaddict

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

Paul
Paul

Reputation: 2769

Just to detect if they're there?

preg_match('/[A-Za-z]/', $subject, $matches_arr);

That should do the trick.

Upvotes: 1

nonopolarity
nonopolarity

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

Related Questions