php ctype函数中文翻译和示例
PHP Ctype扩展是PHP4.2开始就内建的扩展,注意,Ctype系列函数都只有一个字符串类型参数,它们返回布尔值。 代码如下: $str = "0.1123";
//检查字符串所有字符是否为数字 echo "ctype_digit:" . ctype_digit($str); //空 //检测是否为数字字符串,可为负数和小数 echo "is_numberic:" . is_numeric($str); //1 从上面可以看出ctype_digit()和is_numberic()的区别。 中文翻译 Ctype函数是PHP内置的字符串体测函数。主要有以下几种 ctype_alnum -- Check for alphanumeric character(s) ctype_alpha -- Check for alphabetic character(s) ctype_cntrl -- Check for control character(s) ctype_digit -- Check for numeric character(s) ctype_graph -- Check for any printable character(s) except space ctype_lower -- Check for lowercase character(s) ctype_print -- Check for printable character(s) ctype_punct -- Check for any printable character which is not whitespace or an alphanumeric character ctype_space -- Check for whitespace character(s) ctype_upper -- Check for uppercase character(s) ctype_xdigit -- Check for character(s) representing a hexadecimal digit 有示例的哟 我们平常在遇到要对一些表单做简单过滤的时候,往往不太愿意写正则,而且在效率上,正则也是影响PHP运行速度的原因之一,所以在能不试用正则的时候尽量不试用正则。幸好PHP已经为我们考虑到了这一点,给我提供了Ctype函数。下面对一些Ctype函数做一些简单介绍,以备用: 代码如下: $strings = array('AbCd1zyZ9','foo!#$bar');
foreach ($strings as $testcase) { if (ctype_alnum($testcase)) { echo "The string $testcase consists of all letters or digits.n"; 输出The string AbCd1zyZ9 consists of all letters or digits. } else { echo "The string $testcase does not consist of all letters or digits.n"; 输出 The string foo!#$bar does not consist of all letters or digits. } } ?> 2、ctype_alpha — Check for alphabetic character(s) 检查字符串中只包含字母。 成功时返回TRUE,失败为FALSE; 代码如下: $strings = array('KjgWZC','arf12');
foreach ($strings as $testcase) { if (ctype_alpha($testcase)) { echo "The string $testcase consists of all letters.n"; 输出 The string KjgWZC consists of all letters. } else { echo "The string $testcase does not consist of all letters.n"; 输出 The string arf12 does not consist of all letters. } } ?> 3、ctype_cntrl — Check for control character(s) 检查字符串中是否只包含" 'n' 'r' 't' " 这样的控制字符。 代码如下: $strings = array('string1' => "nrt",'string2' => 'arf12');
foreach ($strings as $name => $testcase) { if (ctype_cntrl($testcase)) { echo "The string '$name' consists of all control characters.n"; 输出 The string 'string1' consists of all control characters. } else { echo "The string '$name' does not consist of all control characters.n"; The string 'string2' does not consist of all control characters. } } ?> 4、ctype_digit — Check for numeric character(s) 检查字符串中是否只包含数字 代码如下: $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"; } } ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |