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
  • 71,546
  • 23
  • 135
  • 174
Saint Robson
  • 5,475
  • 18
  • 71
  • 118
  • * 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
  • 71,546
  • 23
  • 135
  • 174
2
if(preg_match("@^\d{4,5}$@", basename($entry, ".php"))) {
--do something here--
}
Pedro del Sol
  • 2,840
  • 9
  • 39
  • 52
2

instead of

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

use

if(preg_match("@^\d{4,5}$@", basename($entry, ".php"))) {
exussum
  • 18,275
  • 8
  • 32
  • 65