php ctype函数中文翻译和示例


Posted in PHP onMarch 21, 2014

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";  
    }  
}  
?>  
PHP 相关文章推荐
PHP的宝库目录--PEAR
Oct 09 PHP
一个简单的PHP投票程序源码
Mar 11 PHP
WindowsXP中快速配置Apache+PHP5+Mysql
Jun 05 PHP
PHP中去除换行解决办法小结(PHP_EOL)
Nov 27 PHP
解析thinkphp的左右值无限分类
Jun 20 PHP
php正则匹配html中带class的div并选取其中内容的方法
Jan 13 PHP
使用PHP如何实现高效安全的ftp服务器(一)
Dec 20 PHP
java模拟PHP的pack和unpack类
Apr 13 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
Oct 10 PHP
php常用正则函数实例小结
Dec 29 PHP
PHP两种实现无级递归分类的方法
Mar 02 PHP
PHP大文件分割上传 PHP分片上传
Aug 28 PHP
php的declare控制符和ticks教程(附示例)
Mar 21 #PHP
php像数组一样存取和修改字符串字符
Mar 21 #PHP
easyui的tabs update正确用法分享
Mar 21 #PHP
php设置session值和cookies的学习示例
Mar 21 #PHP
一个显示效果非常不错的PHP错误、异常处理类
Mar 21 #PHP
一漂亮的PHP图片验证码实例
Mar 21 #PHP
PHP中nowdoc和heredoc使用需要注意的一点
Mar 21 #PHP
You might like
php将session放入memcached的设置方法
2014/02/14 PHP
PHP实现对文本数据库的常用操作方法实例演示
2014/07/04 PHP
PHP移动文件指针ftell()、fseek()、rewind()函数总结
2014/11/18 PHP
Yii框架引入coreseek分页功能示例
2019/02/08 PHP
阻止JavaScript事件冒泡传递(cancelBubble 、stopPropagation)
2007/05/08 Javascript
js 变量类型转换常用函数与代码[比较全]
2009/12/01 Javascript
如何将php数组或者对象传递给javascript
2014/03/20 Javascript
JS实现新浪博客左侧的Blog管理菜单效果代码
2015/10/22 Javascript
jQuery实现鼠标经过事件的延时处理效果
2020/08/20 Javascript
js 判断附件后缀的简单实现方法
2016/10/11 Javascript
jQuery模拟实现的select点击选择效果【附demo源码下载】
2016/11/09 Javascript
jquery submit()不能提交表单的解决方法
2017/04/24 jQuery
JS实现的随机排序功能算法示例
2017/06/09 Javascript
jquery实现企业定位式导航效果
2018/01/01 jQuery
把vue-router和express项目部署到服务器的方法
2018/02/21 Javascript
React 全自动数据表格组件——BodeGrid的实现思路
2019/06/12 Javascript
js实现登录时记住密码的方法分析
2020/04/05 Javascript
jQuery实现的移动端图片缩放功能组件示例
2020/05/01 jQuery
[02:49]DOTA2完美大师赛首日观众采访
2017/11/23 DOTA
python线程池的实现实例
2013/11/18 Python
python实现百度关键词排名查询
2014/03/30 Python
Collatz 序列、逗号代码、字符图网格实例
2017/06/22 Python
Python基于matplotlib绘制栈式直方图的方法示例
2017/08/09 Python
pandas.cut具体使用总结
2019/06/24 Python
python 最简单的实现适配器设计模式的示例
2020/06/30 Python
Urban Decay官方网站:美国化妆品品牌
2020/06/04 全球购物
eHarmony英国:全球领先的认真恋爱约会平台之一
2020/11/16 全球购物
医药专业应届毕业生求职信范文
2014/01/01 职场文书
一般纳税人申请报告
2015/05/18 职场文书
高中16字霸气押韵班级口号集锦!
2019/06/27 职场文书
用Python提取PDF表格的方法
2021/04/11 Python
JavaScript嵌入百度地图API的最详细方法
2021/04/16 Javascript
springboot如何接收application/x-www-form-urlencoded类型的请求
2021/11/02 Java/Android
JAVA SpringMVC实现自定义拦截器
2022/03/16 Python
Python识别花卉种类鉴定网络热门植物并自动整理分类
2022/04/08 Python
SQL试题 使用窗口函数选出连续3天登录的用户
2022/04/24 Oracle