Numbers and commas only

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Numbers and commas only

Post by Mr Tech »

I have a script that I want to use if() to check adn see if the $string has only numbers and commas.

E.g:

if (isnumber($number)) {
echo "it is a number";
} else {
echo "it is not jsut number";
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

are you asking, or offerring to share, or what?
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Meh... sorry, I forgot to ad that. I'm asking :roll:

Sorry I was vague.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

since you didn't specify specifics as to format...

Code: Select all

^[0-9,]+$
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post by Mr Tech »

Thanks feyd.

I put it into this:

Code: Select all

<?php
$what = "2,d67";
if (preg_match("/^[0-9,]+$/is", $what, $match )) {
echo "yep";
} else {
echo "nope";
}
?>
Is that the best way to do it?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

I usually do it like:

Code: Select all

if ($what == strval(floatval($what))
{
  //
}
Post Reply