欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php ctype函数中文翻译和示例

程序员文章站 2023-11-11 19:35:46
php ctype扩展是php4.2开始就内建的扩展,注意,ctype系列函数都只有一个字符串类型参数,它们返回布尔值。 复制代码 代码如下:$str = "0.1123...

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)
检测是否是只包含[a-za-z0-9]

ctype_alpha -- check for alphabetic character(s)
检测是否是只包含[a-za-z]

ctype_cntrl -- check for control character(s)
检查是否是只包含类是“\n\r\t”之类的字 符控制字符

ctype_digit -- check for numeric character(s)
检查时候是只包含数字字符的字符串(0-9)

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
检查是否是16进制的字符串,只能包括 “0123456789abcdef”

有示例的哟

我们平常在遇到要对一些表单做简单过滤的时候,往往不太愿意写正则,而且在效率上,正则也是影响php运行速度的原因之一,所以在能不试用正则的时候尽量不试用正则。幸好php已经为我们考虑到了这一点,给我提供了ctype函数。下面对一些ctype函数做一些简单介绍,以备用:
1、ctype_alnum — check for alphanumeric character(s)   检查字符串中只包含数字或字母,相当于正则[a-za-z0-9].   有返回值。成功时返回true,失败为false;
[

复制代码 代码如下:

<?php 
$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;

复制代码 代码如下:

<?php 
$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";<span style="white-space:pre">   </span>\\ 输出 the string arf12 does not consist of all letters. 
    } 

?> 

3、ctype_cntrl — check for control character(s)    检查字符串中是否只包含" '\n' '\r' '\t' " 这样的控制字符。

复制代码 代码如下:

<?php 
$strings = array('string1' => "\n\r\t", '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) 检查字符串中是否只包含数字

复制代码 代码如下:

<?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"; 
    } 

?>