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 = ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |