RegExp

Possible inputs:

{d}= digit(s)

{d}.{d}.{d}.{d} AND {d}.{d}.{d}

Now how can I validate that $x is valid?

BTW {d} could equal 23746983268345687346 or 3, but not -658

Steven
LVL 5
mnb93Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

m1tk4Commented:
preg_match('/^\d+\.\d+\.\d+(\d+\.)?$/', $x)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
neesterCommented:
if (preg_match("/[0-9+]/",$x) {
  //VALID
}else{
  //INVALID
}
neesterCommented:
Sorry,

 if (preg_match("/^[0-9]+$/",$x) {
  //VALID
}else{
  //INVALID
}


[0-9]+
means any numbers between 0 and 9.
The + means there must be atleast 1 or more.
and the ^ and $ mean it must span the whole block.
(^=start, $=end)
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

mnb93Author Commented:
m1tk4's way looks correct, however neester's way doesn't look correct to me, how would: /^[0-9]+$/",$x parse true this: 2.3.4.5?

Thanks.
tolgaongCommented:
$number="658";

if (preg_match('/^\\d+$/', $number)) echo "VALID";
else echo "NOT VALID";
neesterCommented:
Awww sorry!

I read this wrong:

"BTW {d} could equal 23746983268345687346 or 3, but not -658"


This will work :)

if (preg_match("/^([0-9]|\.)+$/",$x) {
  //VALID
}else{
  //INVALID
}
tdterryCommented:
How about this:

if (preg_match("/^[\d\.]+$/", $x) {
  // $x is only digits and dots
  $digits = explode('.', $s);
  // now you can use $digits[0], [1], [2], etc to get the blocks of digits (see warning below)
} else {
  // not match
}

This is similar to neester's proposed solution but goes a step further to separate the digits.  Warning: Both my and neester's comments have a similar "issue" that they accept {d}..{d} with multiple period separators.  If single periods are a must and ONLY three or four blocks of digits are allowed, m1k4 was ALMOST correct.  His last grouping had the period and \d reversed.   Should be:

preg_match("/^\d+\.\d+\.\d+(\.\d+)?$/", $x);
m1tk4Commented:
preg_match("/^[\d\.]+$/", $x)  will also match ...3.2.


preg_match("/^\d+\.\d+\.\d+(\.\d+)?$/", $x); is a correct answer
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.