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 相关文章推荐
一个改进的UBB类
Oct 09 PHP
第一节--面向对象编程
Nov 16 PHP
解析PHP计算页面执行时间的实现代码
Jun 18 PHP
怎样使用php与jquery设置和读取cookies
Aug 08 PHP
php calender(日历)二个版本代码示例(解决2038问题)
Dec 24 PHP
19个超实用的PHP代码片段
Mar 14 PHP
php中explode的负数limit用法分析
Feb 27 PHP
PHP实现简单搜歌的方法
Jul 28 PHP
PHP实现的一致性哈希算法完整实例
Nov 14 PHP
php curl优化下载微信头像的方法总结
Sep 07 PHP
php多进程模拟并发事务产生的问题小结
Dec 07 PHP
在laravel框架中使用model层的方法
Oct 08 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 时间设定)使用入门代码
2008/06/05 PHP
Base64在线编码解码实现代码 演示与下载
2011/01/08 PHP
浅谈PHP强制类型转换,慎用!
2013/06/06 PHP
php缩小png图片不损失透明色的解决方法
2013/12/25 PHP
ThinkPHP3.1新特性之命名范围的使用
2014/06/19 PHP
浅谈php安全性需要注意的几点事项
2014/07/17 PHP
PHP实现的下载远程图片自定义函数分享
2015/01/28 PHP
Ubuntu12下编译安装PHP5.3开发环境
2015/03/27 PHP
jQuery EasyUI API 中文文档 - NumberBox数字框
2011/10/13 Javascript
drag-and-drop实现图片浏览器预览
2015/08/06 Javascript
Javascript中判断一个值是否为undefined的方法详解
2016/09/28 Javascript
利用iscroll4实现轮播图效果实例代码
2017/01/11 Javascript
教你快速搭建Node.Js服务器的方法教程
2017/03/30 Javascript
vue init webpack myproject构建项目 ip不能访问的解决方法
2018/03/20 Javascript
泛谈JS逻辑判断选择器 || &amp;&amp;
2019/05/24 Javascript
微信小程序sessionid不一致问题解决
2019/08/30 Javascript
Vue表单提交点击事件只允许点击一次的实例
2020/10/23 Javascript
[47:22]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
[46:40]VGJ.T vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
Python 查看文件的编码格式方法
2017/12/21 Python
python文件读写代码实例
2019/10/21 Python
python离线安装外部依赖包的实现
2020/02/13 Python
什么是python的必选参数
2020/06/21 Python
pip已经安装好第三方库但pycharm中import时还是标红的解决方案
2020/10/09 Python
团支书的期末学习总结自我评价
2013/11/01 职场文书
大学毕业生最详细的自我评价分享
2013/11/18 职场文书
汽车专业大学生职业生涯规划范文
2014/01/07 职场文书
一句话工作感言
2014/03/01 职场文书
《鲁班和橹板》教学反思
2014/04/27 职场文书
文员试用期转正自我鉴定
2014/09/14 职场文书
捐款感谢信
2015/01/20 职场文书
敬老院志愿者活动总结
2015/05/06 职场文书
长征观后感
2015/06/09 职场文书
学术研讨会主持词
2015/07/04 职场文书
iSCSI服务器CHAP双向认证配置
2022/04/01 Servers
Spring IOC容器Bean的作用域及生命周期实例
2022/05/30 Java/Android