php - Preg_match and substr together -
i have string need test see if characters after [0] character (the first) numbers or not.
so example :
c [0]
i need check if remaining characters numbers meaning after c :
12345678
making string c12345678
this have tried :
elseif(substr($str, 1, preg_match("/^[0-9]/", $str) === false)) { $this -> set_message('is_valid_student_id', 'must have 8 digits 0 - 9'); return false; }
the first character character last 8 numeric.
thanks
edit 1 :
so doing guys told me not giving me expected outcome...
elseif (preg_match("/^\c\d{8}$/i", $str) === false) // lower case cc + 8 digits { $this -> set_message('is_valid_student_id', 'must have 8 digits 0 - 9'); return false; }
is not returning false if type in letter after first letter [c or c]
how non-regex solution?
$student_id = "c123b5678"; if(ctype_alpha($student_id[0])){ if(ctype_digit(substr($student_id,1,strlen($student_id) - 1))){ echo 'valid'; } else{ echo 'invalid'; } } else { echo 'invalid starting character'; } // echoes invalid
edit: pointed out cryode, is_numeric()
far broad application. allow strings c1e5
pass valid... using ctype_digit()
fixes issue. if desired, add check strlen()
well.
Comments
Post a Comment