Jump to content

Match a number of integers in a string


Mido

Recommended Posts

Hello, I'm a beginner in regex. Basically, what I want is to match a specified number of integers in a string. Like, :

$str="123-456-789" // phone number... 

I want to check, via preg_match(), whether the string contains [7-15] digits. So, 123-456-789-321-654 would be valid; they contain 20 characters, but only 15 digits, so it's within the interval range.

 

I thought about validating the string then stripping out the hyphens/spaces then count the final filtered version of the string. But I'd rather do them all in one regex pattern. Is there a way to do that?

 

If it wasn't clear enough, please tell which part wasn't. Thanks.

Link to comment
https://forums.phpfreaks.com/topic/209110-match-a-number-of-integers-in-a-string/
Share on other sites

Well OK, that's a like it is *technically* possible...

 

 

$pattern = '#^[^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9][^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*[0-9]?[^0-9]*$#';

  • 3 weeks later...

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.