1

I have this regex :

if(preg_match("@^\d{4}$@", basename($entry, ".php"))) {
--do something here--
}

that condition works only for 4 digits number. but I need to validate 4 digits and also 5 digits. how to make it work to validate 5 digits number too? thanks!

Jason McCreary
  • 69,176
  • 21
  • 125
  • 169
Saint Robson
  • 5,364
  • 17
  • 65
  • 111
  • * See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a tutorial. – mario Nov 07 '12 at 16:32

4 Answers4

6

the braces can take a low and high end of a range so {4,5} should work.

LazyMonkey
  • 517
  • 5
  • 8
3

As an alternative to Regular Expressions, consider simpler functions like ctype_digit() and strlen().

$filename = basename($entry, ".php");
$length = strlen($filename);

if (($length >= 4 && $length <= 5) && ctype_digit($filename)) {
  // your code
}
Jason McCreary
  • 69,176
  • 21
  • 125
  • 169
2
if(preg_match("@^\d{4,5}$@", basename($entry, ".php"))) {
--do something here--
}
Pedro del Sol
  • 2,773
  • 9
  • 43
  • 50
2

instead of

 if(preg_match("@^\d{4}$@", basename($entry, ".php"))) {

use

if(preg_match("@^\d{4,5}$@", basename($entry, ".php"))) {
exussum
  • 17,675
  • 8
  • 30
  • 64