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 相关文章推荐
使用Limit参数优化MySQL查询的方法
Nov 12 PHP
PHP $_FILES函数详解
Mar 09 PHP
php安全之直接用$获取值而不$_GET 字符转义
Jun 03 PHP
PHP中配置IIS7实现基本身份验证的方法
Sep 24 PHP
PHP+MySQL实现的简单投票系统实例
Feb 24 PHP
PHP的数组中提高元素查找与元素去重的效率的技巧解析
Mar 03 PHP
php中foreach结合curl实现多线程的方法分析
Sep 22 PHP
YII框架批量插入数据的方法
Mar 18 PHP
PHP7下协程的实现方法详解
Dec 17 PHP
PHP微信发送推送消息乱码的解决方法
Feb 28 PHP
PHP生成指定范围内的N个不重复的随机数
Mar 18 PHP
PHP实现读取文件夹及批量重命名文件操作示例
Apr 15 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优化那些事(经验分享)
2014/11/27 PHP
PHP实现的简单mock json脚本分享
2015/02/10 PHP
php计算一个文件大小的方法
2015/03/30 PHP
大家在抢红包,程序员在研究红包算法
2015/08/31 PHP
在PHP站点的页面上添加Facebook评论插件的实例教程
2016/01/08 PHP
php  单例模式详细介绍及实现源码
2016/11/05 PHP
Laravel框架基于ajax实现二级联动功能示例
2019/01/17 PHP
JavaScript 检测浏览器和操作系统的脚本
2008/12/26 Javascript
前淘宝前端开发工程师阿当的PPT中有JS技术理念问题
2010/01/15 Javascript
前端开发必须知道的JS之原型和继承
2010/07/06 Javascript
两个listbox实现选项的添加删除和搜索
2013/03/01 Javascript
windows8.1+iis8.5下安装node.js开发环境
2014/12/12 Javascript
jQuery制作简洁的多级联动Select下拉框
2014/12/23 Javascript
动态生成的DOM不会触发onclick事件的原因及解决方法
2016/08/06 Javascript
JavaScript实现分页效果
2017/03/28 Javascript
Vue.js常用指令的使用小结
2017/06/23 Javascript
JS和jQuery通过this获取html标签中的属性值(实例代码)
2017/09/11 jQuery
如何让node运行es6模块文件及其原理详解
2018/12/11 Javascript
ajaxfileupload.js实现上传文件功能
2019/04/19 Javascript
layui自定义验证,用ajax查询后台是否有重复数据,form.verify的例子
2019/09/06 Javascript
微信小程序列表时间戳转换实现过程解析
2019/10/12 Javascript
JavaScript判断数组类型的方法
2019/10/23 Javascript
[05:41]2014DOTA2西雅图国际邀请赛 小组赛7月10日TOPPLAY
2014/07/10 DOTA
python3监控CentOS磁盘空间脚本
2018/06/21 Python
对python多线程与global变量详解
2018/11/09 Python
图文详解python安装Scrapy框架步骤
2019/05/20 Python
kali中python版本的切换方法
2019/07/11 Python
使用pip安装python库的多种方式
2019/07/31 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
如何利用python web框架做文件流下载的实现示例
2020/06/02 Python
HTML5新增元素如何兼容旧浏览器有哪些方法
2014/05/09 HTML / CSS
英国最大的电子产品和家电零售企业:Currys PC World
2016/09/24 全球购物
俄罗斯GamePark游戏商店网站:购买游戏、游戏机和配件
2020/03/13 全球购物
汽车专业毕业生自荐信
2013/11/03 职场文书
网页美工求职信
2014/02/15 职场文书