How to match numbers ranging from 400 to 550 with regex?

I want to validate a numeric string that must only contain numbers ranging from 400 to 550.

$x = "401";  // valid 
$x = "551";  // invalid 

I want to only match the numbers between 400 and 550.

I tried the following pattern:

if(preg_match_all("/[400-550]+/",$x)); 

But it doesn't work.

What is the regex pattern for matching numbers 400-550?

Answers


A regex-way to validate this range:

\b(?:4[0-9]{2}|5[0-4][0-9]|550)\b

See demo. It will also work in larger texts since \b boundaries are used. In case of whole strings, use ^ and $ around instead of \b.


Need Your Help

session authentication between ajax and php

php javascript html ajax session

I'm wondering what the typical way storing a session would be, so that you could not just typing in www.website.com/inserter.php?this=whatever and insert something into a database. Currently I do ...

Fill two image overlay like seekbar on touch event of image-view

android android-imageview seekbar gauge

There are two image black and blue while touching on blue image it should fill like progress, I achieved using multiple cuts images without using canvas but not get smoothness on touch, like 100 Pu...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.