$var = 3;
if ($cleanvar = preg_match('/^[0-9]{0,2}$/', $var))
{
echo $cleanvar; echo $var; exit();
}
else
. . .
Strange output. This is causing my cleanvar to echo 1 and var still echo’s 3. Why is this happening? The point of this regex is to only match whole numbers, as 1 or 2 digits. e.g.( 1, 2, 4, 38, 24)
Is their an issue in my Regex? Or what is causing this odd behavior?
$cleanvaris just true or false. You’re looking for a number between 0 and 2 digits in length (from the beginning to the end of the string, so no other characters are allowed).EDIT: it returns 1 if matched, 0 if not.
See this for more information: https://www.php.net/preg_match