1

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

0

2 Answers 2

3

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))
Sign up to request clarification or add additional context in comments.

6 Comments

My bad - I forgot the system I was developing for was A-Z not a-z, however I do want to support both.
Does the /i account for a-z also
@AlexZ Both parts of my answer account for lower case characters.
Thanks @Jack, the second option seems more readable for future reference, as the /i may easily be skipped.
I notice laravel use github.com/laravel/framework/blob/master/src/Illuminate/… the /u does this account for uppercase I assume?
|
-1

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);
}

2 Comments

This doesn't seem to work either sandbox.onlinephpfunctions.com/code/…
But those are outside of your ASCII range, try it with uppercase JONES 1234 or change your hexadecimal values to extend it to lower case characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.