Remove all non-printable characters from a string:
<?php
$str = implode('', array_filter(str_split($str, 1), 'ctype_print'));
?>
ctype_digit
(PHP 4 >= 4.0.4, PHP 5)
ctype_digit — 数字かどうかを調べる
説明
bool ctype_digit
( string $text
)
与えられた文字列 text のすべての文字が 数字であるかどうかを調べます。
パラメータ
- text
-
調べる文字列。
返り値
string text のすべての文字が 10 進数字だった場合に TRUE、そうでない場合に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 | PHP 5.1.0 より前のバージョンでは、この関数は text が空文字列の場合に TRUE を返していました。 |
例
例1 ctype_digit() の例
<?php
$strings = array('1820.20', '10002', 'wsl!12');
foreach ($strings as $testcase) {
if (ctype_digit($testcase)) {
echo "The string $testcase consists of all digits.\n";
} else {
echo "The string $testcase does not consist of all digits.\n";
}
}
?>
上の例の出力は以下となります。
The string 1820.20 does not consist of all digits. The string 10002 consists of all digits. The string wsl!12 does not consist of all digits.
例2 ctype_digit() で文字列と整数値を比較する例
<?php
$numeric_string = '42';
$integer = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer); // false
is_numeric($numeric_string); // true
is_numeric($integer); // true
?>
注意
注意: この関数を活用するには string を渡さなければなりません。 たとえば integer を渡すと常に FALSE を返します。 HTML フォームに入力された整数値は、integer ではなく string 型で返されます。 マニュアルの 型 についての節を参照ください。
参考
- ctype_alnum() - 英数字かどうかを調べる
- ctype_xdigit() - 16 進数を表す文字かどうかを調べる
- is_numeric() - 変数が数字または数値形式の文字列であるかを調べる
- is_int() - 変数が整数型かどうかを検査する
- is_string() - 変数の型が文字列かどうかを調べる
ctype_digit
Chris
02-Aug-2009 04:17
02-Aug-2009 04:17
raul dot 3k at gmail dot com
10-Apr-2009 12:21
10-Apr-2009 12: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
20-Nov-2008 06:56
20-Nov-2008 06: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 11:43
10-Sep-2007 11: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;
}
?>
