downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

ctype_graph> <ctype_cntrl
Last updated: Fri, 30 Oct 2009

view this page in

ctype_digit

(PHP 4 >= 4.0.4, PHP 5)

ctype_digitAuf Ziffern überprüfen

Beschreibung

bool ctype_digit ( string $text )

Prüft ob der übergebene String nur aus Ziffern besteht.

Parameter-Liste

text

Der zu prüfende String.

Rückgabewerte

Liefert TRUE wenn jedes Zeichen in text eine Ziffer ist, ansonsten FALSE.

Beispiele

Beispiel #1 ctype_digit() Beispiel

<?php
$strings 
= array('1820.20''10002''wsl!12');
foreach (
$strings as $testcase) {
    if (
ctype_digit($testcase)) {
        echo 
"Der String $testcase besteht aus Ziffern.\n";
    } else {
        echo 
"Der String $testcase enthält nicht nur Ziffern.\n";
    }
}
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

Der String 1820.20 enthält nicht nur Ziffern.
Der String 10002 besteht aus Ziffern.
Der String wsl!12 enthält nicht nur Ziffern.

Siehe auch



ctype_graph> <ctype_cntrl
Last updated: Fri, 30 Oct 2009
 
add a note add a note User Contributed Notes
ctype_digit
Chris
02-Aug-2009 07:17
Remove all non-printable characters from a string:
<?php
$str
= implode('', array_filter(str_split($str, 1), 'ctype_print'));
?>
Peter de Pijd
18-Jul-2009 10:53
Note that an empty string is also false:
ctype_digit("") // false
a_p_leeming at hotmail dot com
25-May-2009 01:17
Also note that

<?php ctype_digit("-1");   //false ?>
raul dot 3k at gmail dot com
09-Apr-2009 03:21
The ctype_digit can be used in a simple form to validate a field:
<?php
$field
= $_POST["field"];
if(!
ctype_digit($field)){
  echo
"It's not a digit";
}
?>

Note:
Digits is 0-9
Anonymous
19-Nov-2008 09:56
Indeed, ctype_digit only functions correctly on strings. Cast your vars to string before you test them. Also, be wary and only use ctype_digit if you're sure your var contains either a string or int, as boolean true for ex will convert to int 1.

To be truly safe, you need to check the type of the var first. Here's a wrapper function that improves upon ctype_digit's broken implementation:

<?php

// replacement for ctype_digit, to properly
// handle (via return value false) nulls,
// booleans, objects, resources, etc.
function ctype_digit2 ($str) {
    return (
is_string($str) || is_int($str) || is_float($str)) &&
       
ctype_digit((string)$str);
}

?>

If, like me, you're not willing to take a chance on ctype_digit having other problems, use this version:

<?php

// replacement for ctype_digit, to properly
// handle (via return value false) nulls,
// booleans, objects, resources, etc.
function ctype_digit2 ($str) {
    return (
is_string($str) || is_int($str) || is_float($str)) &&
       
preg_match('/^\d+\z/', $str);
}

?>
minterior at gmail dot com
10-Sep-2007 02:43
I use ctype_digit() function as a part of this IMEI validation function.

<?php

/**
 * Check the IMEI of a mobile phone
 * @param $imei IMEI to validate
 */
function is_IMEI_valid($imei){   
    if(!
ctype_digit($imei)) return false;
   
$len = strlen($imei);
    if(
$len != 15) return false;

    for(
$ii=1, $sum=0 ; $ii < $len ; $ii++){
        if(
$ii % 2 == 0) $prod = 2;
        else
$prod = 1;
       
$num = $prod * $imei[$ii-1];
        if(
$num > 9){
         
$numstr = strval($num);
         
$sum += $numstr[0] + $numstr[1];
        }else
$sum += $num;
    }

   
$sumlast = intval(10*(($sum/10)-floor($sum/10))); //The last digit of $sum
   
$dif = (10-$sumlast);
   
$diflast = intval(10*(($dif/10)-floor($dif/10))); //The last digit of $dif
   
$CD = intval($imei[$len-1]); //check digit

   
if($diflast == $CD) return true;

    return
false;
}
?>

ctype_graph> <ctype_cntrl
Last updated: Fri, 30 Oct 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites