Jump to content

preg_match working in one place but not another?


jamesxg1

Recommended Posts

Hiya peeps!

 

I have this code;

 

				if(preg_match('([0-9]{5})', $this->_searchString)) {
					echo '5 characters, numbers only.';
				} elseif(preg_match('([0-9]{7})', $this->_searchString)) {
					echo '7 characters, numbers only.';
				}

 

The 7 characters are not working for some reason, I have used this preg_match regex all over my script and it's working everywhere else but with this?

 

Can anyone see any reason why it is not working?

 

Many thanks,

 

James.

You should check for 7 digits first. Because this [0-9]{5} will match: 12345 AND 1234567 therefore your script never reaches the 1234567 ( [0-9]{7} ) because 5 digits is matched first.

 

Wow! I honestly didn't think of that! If I add $ at the end of each regex will this stop that?

 

Many thanks,

 

James.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.