David montera
David montera

Reputation: 19

how to get equivalent of a number with restrict

Hello guys I want to add some restrict inside my condition in a function for a number, and I want to accept a number with just these criteria :

-"58xxx" with '58' in the first
-also accept it with space " 58xxx" or without space "58xxx"

how can i do that on php

i have done this before is it correct ?

'\[1]{1}[2]{5}[1-9]{3}'

Upvotes: 0

Views: 30

Answers (1)

Markus Zeller
Markus Zeller

Reputation: 9090

You can make use of a simple regex. This will test for

  • any number of spaces
  • '58'
  • 3 digits
$tests = [
  ' 58123',
  '     58123',
  '58123',
  '158123',
  '15813',
  ' 15813',
];

foreach($tests as $test) {
    echo $test, ': ', preg_match('/\s*58(\d{3})/', $test) ? 'true' : 'false', PHP_EOL;
}
58123: true
     58123: true
58123: true
158123: true
15813: false
 15813: false

Upvotes: 1

Related Questions