Preg_match

Sir, I have these codes


$re='31201-1613227-1';
if(preg_match('/^\d{5}-\d{7}-\d{1}/',$re)){
	echo "Valid";
}else{
	echo "Not Valid";
}

It work fine but if I use this value in variable

$re='31205-1653229-156588';

then it also say: Valid

I need following more validations on this string

  1. If total length is 15 then it must say VALID
  2. If first digit is 3 then it must say VALID, if first digit is not 3 then NOT VALID

How to modify this condition

if(preg_match('/^\d{5}-\d{7}-\d{1}/',$re)){

Please

Try testing the Rebel expression in the following:

Of course it says valid because it finds the match. If you want a string to be exact match, you have to delimit your pattern as well, adding string start and string end tokens. The string start is already in place, so you have to and only other one:

'/^\d{5}-\d{7}-\d$/'

If first digit is 3 then it must say VALID, if first digit is not 3 then NOT VALID

why not to write it in your patter then?

'/^3\d{4}-\d{7}-\d$/'
1 Like

Thanks sir it done

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.