Re: Re: How to check if a *string* has only numbers...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Better yet, use the right function :-)

This script

<?php

   $a = "1234567890";
   $b = $a.$a;
   $c = $a."A";

   echo "$a ";
   if (!is_numeric($a))
       echo "NOT ";
   echo "numeric<br>";

   echo "$b ";
   if (!is_numeric($b))
       echo "NOT ";
   echo "numeric<br>";

   echo "$c ";
   if (!is_numeric($c))
       echo "NOT ";
   echo "numeric<br>";

?>

should provide this output

1234567890 numeric
12345678901234567890 numeric
1234567890A NOT numeric


Luis


user@xxxxxxxxxxxxxx wrote:

Hi,

Have you tried regular expressions ? try them, they might do the trick

Rui Francisco

Rafael Soares wrote:

Hello people!!!



I would like to check if a string contains only numbers, I really can’t
realize how to do it.



I tried is_int(), but it don’t work because the variable is a string (I used
gettype() to check it) and is_int() returns NULL…




It is to validate some data in a form. The variable I’m trying to test is
the result of this script:




$data = $_POST[‘data’]; // something like 123.456.789

$dataParts = explode($data);

//these are the variable I have to check:

$dataP1 = $dataParts[0];

$dataP2 = $dataParts[1];

$dataP3 = $dataParts[3];



gettype($dataP1), gettype($dataP2) and gettype($dataP3) return string

gettype($dataParts) returns array



Thanks in advance.



Rafael Soares - AgênciaM

Fone:  +55 11 4616-1394






-- 
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux