Check String for Digits ?

This is a discussion on Check String for Digits ? within the PHP Language forums, part of the PHP Programming Forums category; Hi guys, I want to validate a string to make sure it contains only digits. 0001 = true 6669 = true 00a0 = ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-19-2004
Cyrus D.
 
Posts: n/a
Default Check String for Digits ?

Hi guys,

I want to validate a string to make sure it contains only digits.

0001 = true
6669 = true
00a0 = false
6.665 = false

What's the best way to go about doing that ?

Take care,
Cyrus


Reply With Quote
  #2 (permalink)  
Old 10-19-2004
Bent Stigsen
 
Posts: n/a
Default Re: Check String for Digits ?

Cyrus D. wrote:
> Hi guys,
>
> I want to validate a string to make sure it contains only digits.
>
> 0001 = true
> 6669 = true
> 00a0 = false
> 6.665 = false
>
> What's the best way to go about doing that ?


Lots of ways to do it.
This is one way: preg_match('/^\d{4,4}$/', $var)

/Bent
Reply With Quote
  #3 (permalink)  
Old 10-19-2004
Justin Koivisto
 
Posts: n/a
Default Re: Check String for Digits ?

Cyrus D. wrote:

> Hi guys,
>
> I want to validate a string to make sure it contains only digits.
>
> 0001 = true
> 6669 = true
> 00a0 = false
> 6.665 = false
>
> What's the best way to go about doing that ?
>
> Take care,
> Cyrus
>
>


preg_match('`^[0-9]+$`',$string);

returns TRUE if *only* digits are in the string, no matter what length
it is.

--
Justin Koivisto - spam@koivi.com
http://www.koivi.com
Reply With Quote
  #4 (permalink)  
Old 10-19-2004
Pedro Graca
 
Posts: n/a
Default Re: Check String for Digits ?

Cyrus D. wrote:
> I want to validate a string to make sure it contains only digits.

[snip]
> What's the best way to go about doing that ?



I'd use a regular expression:

php$ cat onlydigits.php
<?php
function only_digits($x) {
return !preg_match('/[^0-9]/', $x);
}

if (only_digits('0001')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('6669')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('00a0')) echo 'OK'; else echo 'BAD'; echo "\n";
if (only_digits('6.665')) echo 'OK'; else echo 'BAD'; echo "\n";
?>

php$ php onlydigits.php
OK
OK
BAD
BAD



Check the manual @
http://www.php.net/preg_match

--
USENET would be a better place if everybody read:
http://www.expita.com/nomime.html
http://www.netmeister.org/news/learn2quote2.html
http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote
  #5 (permalink)  
Old 10-20-2004
Markus Ernst
 
Posts: n/a
Default Re: Check String for Digits ?

Cyrus D. wrote:
> Hi guys,
>
> I want to validate a string to make sure it contains only digits.
>
> 0001 = true
> 6669 = true
> 00a0 = false
> 6.665 = false
>
> What's the best way to go about doing that ?


An alternative suggestion without regexps:

is_numeric($string) && strval(floor(intval($string))) == $string

Checks if $string is numeric; and then removes possible decimal places and
compares that to the original (to make sure there are no points).

--
Markus


Reply With Quote
  #6 (permalink)  
Old 10-20-2004
Matt Raines
 
Posts: n/a
Default Re: Check String for Digits ?

On Wed, 20 Oct 2004, Markus Ernst wrote:

> Cyrus D. wrote:
>> I want to validate a string to make sure it contains only digits.

>
> An alternative suggestion without regexps:
>
> is_numeric($string) && strval(floor(intval($string))) == $string
>
> Checks if $string is numeric; and then removes possible decimal places and
> compares that to the original (to make sure there are no points).


Technically this will also allow strings which begin with "-" or "+", and
those which contain a "." followed solely by zeros. This might be what is
required, though.

--
Matt
Reply With Quote
  #7 (permalink)  
Old 10-21-2004
Markus Ernst
 
Posts: n/a
Default Re: Check String for Digits ?

Matt Raines wrote:
> On Wed, 20 Oct 2004, Markus Ernst wrote:
>
>> Cyrus D. wrote:
>>> I want to validate a string to make sure it contains only digits.

>>
>> An alternative suggestion without regexps:
>>
>> is_numeric($string) && strval(floor(intval($string))) == $string
>>
>> Checks if $string is numeric; and then removes possible decimal
>> places and compares that to the original (to make sure there are no
>> points).

>
> Technically this will also allow strings which begin with "-" or "+",
> and those which contain a "." followed solely by zeros. This might be
> what is required, though.


Well it is rather a mistake in my thinking... a corrected version:

is_numeric($string) && number_format(abs($string), 0, "", "") === $string

--
Markus


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:39 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0