AlexZ
AlexZ

Reputation: 254

Regex Match Within Range

I need do a regex match for ASCII characters 32 - 90 inclusive.

I've attempted the following, however it doesn't seem to return anything.

preg_match("/^[\x20-\x5A]+$/u", $input)

The idea is that it is from hex 20 to hex 5a. I pulled these from http://www.asciitable.com/

I've got a spot for testing this on http://www.phpliveregex.com/p/2Dh

Upvotes: 1

Views: 86

Answers (2)

Ja͢ck
Ja͢ck

Reputation: 173542

Your current range only supports upper case letters, so you need the /i modifier:

$input = 'adddd ### AAAA????';
preg_match('/^[\x20-\x5A]+$/i', $input); // int(1)

Alternatively, add the extra letters in the range:

preg_match('/^[\x20-\x5A\x61-\x7A]+$/', $input))

Upvotes: 3

Scuzzy
Scuzzy

Reputation: 12322

You need to use preg_match's third parameter to assign it to a variable

preg_match("/^[\x20-\x5A]+$/u", $input, $matches)

The standard return of this function is a 1 or 0/FALSE

eg...

if(preg_match("/^[\x20-\x5A]+$/u", $input, $matches))
{
  var_dump($matches);
}

Upvotes: -1

Related Questions