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 截取字符串 分别适合GB2312和UTF8编码情况
Feb 12 PHP
PHP生成网页快照 不用COM不用扩展.
Feb 11 PHP
php curl的深入解析
Jun 02 PHP
md5 16位二进制与32位字符串相互转换示例
Dec 30 PHP
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
Nov 29 PHP
php+Mysqli利用事务处理转账问题实例
Feb 11 PHP
PHP实现获取客户端IP并获取IP信息
Mar 17 PHP
php中array_unshift()修改数组key注意事项分析
May 16 PHP
Yii框架批量插入数据扩展类的简单实现方法
May 23 PHP
PHP使用函数用法详解
Sep 30 PHP
PHP implode()函数用法讲解
Mar 08 PHP
Laravel 数据库加密及数据库表前缀配置方法
Oct 10 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
frename PHP 灵活文件命名函数 frename
2009/09/09 PHP
PHP+MySQL 手工注入语句大全 推荐
2009/10/30 PHP
PHP中的魔术方法总结和使用实例
2015/05/11 PHP
js 自定义的联动下拉框
2010/02/07 Javascript
js隐藏与显示回到顶部按钮及window.onscroll事件应用
2013/01/25 Javascript
绑定回车enter事件代码
2014/05/18 Javascript
js实现日历的简单算法
2017/01/24 Javascript
提升node.js中使用redis的性能遇到的问题及解决方法
2018/10/30 Javascript
Vue2.x Todo之自定义指令实现自动聚焦的方法
2019/01/08 Javascript
用element的upload组件实现多图片上传和压缩的示例代码
2019/02/12 Javascript
vue项目前端埋点的实现
2019/03/06 Javascript
读懂CommonJS的模块加载
2019/04/19 Javascript
JavaScript实现简单的计算器
2020/01/16 Javascript
python使用urllib2模块获取gravatar头像实例
2013/12/18 Python
Python中实现参数类型检查的简单方法
2015/04/21 Python
Python缩进和冒号详解
2016/06/01 Python
Python爬取京东的商品分类与链接
2016/08/26 Python
Python实现PS滤镜碎片特效功能示例
2018/01/24 Python
Python matplotlib绘图可视化知识点整理(小结)
2018/03/16 Python
python3编写ThinkPHP命令执行Getshell的方法
2019/02/26 Python
Python实现EXCEL表格的排序功能示例
2019/06/25 Python
Python实现手机号自动判断男女性别(实例解析)
2019/12/22 Python
python 安装库几种方法之cmd,anaconda,pycharm详解
2020/04/08 Python
HTML5实现移动端弹幕动画效果
2019/08/01 HTML / CSS
澳大利亚现代波西米亚风格女装网站:Bohemian Traders
2018/04/16 全球购物
沃尔玛加拿大:Walmart.ca
2020/03/02 全球购物
招标承诺书
2014/08/30 职场文书
违反交通法规检讨书
2014/09/10 职场文书
党员学习中共十八大报告思想汇报
2014/09/15 职场文书
2015年手术室工作总结
2015/05/11 职场文书
幼儿园亲子活动感想
2015/08/07 职场文书
如何用python绘制雷达图
2021/04/24 Python
Python机器学习之基于Pytorch实现猫狗分类
2021/06/08 Python
springboot中一些比较常用的注解总结
2021/06/11 Java/Android
压缩Redis里的字符串大对象操作
2021/06/23 Redis
openstack中的rpc远程调用的方法
2021/07/09 Python