用于在帖子字段中过滤输入的正则表达式

The only possible value this field can have is P followed by a 0,1,2,3,4 and 0,1,2,3,4

$option_print = $this->input->post('print');

if (!preg_match('^/p[0-4][0-4]/', $option_print)) 
{ redirect('#example'); } else { ... }

Example of VALID inputs:

  • p00 -> valid
  • p10 -> valid

Example of INVALID inputs:

  • v00 -> invalid
  • f10 -> invalid
  • ffs -> invalid

So how should my regex be properly structured to filter for VALID inputs?

How about this:

!preg_match('/^p[0-4][0-4]$/', $option_print)

Or even this:

!preg_match('/^p[0-4]{2}$/', $option_print)

Your regex is almost perfectly valid. But you put ^ before / that why it doesn't work

'/^p[0-4][0-4]$/'