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 相关文章推荐
mysql 搜索之简单应用
Apr 27 PHP
php读取目录所有文件信息dir示例
Mar 18 PHP
Codeigniter整合Tank Auth权限类库详解
Jun 12 PHP
PHP使用Session遇到的一个Permission denied Notice解决办法
Jul 30 PHP
Yii入门教程之目录结构、入口文件及路由设置
Nov 25 PHP
ThinkPHP自动完成中使用函数与回调方法实例
Nov 29 PHP
ecshop后台编辑器替换成ueditor编辑器
Mar 03 PHP
php通过修改header强制图片下载的方法
Mar 24 PHP
CodeIgniter删除和设置Cookie的方法
Apr 07 PHP
php微信公众平台示例代码分析(二)
Dec 06 PHP
php 浮点数比较方法详解
May 05 PHP
PHP7变量处理机制修改
Mar 09 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
解决更换PHP5.4以上版本后Dedecms后台登录空白问题的方法
2015/10/23 PHP
php发送短信验证码完成注册功能
2015/11/24 PHP
php array_slice 取出数组中的一段序列实例
2016/11/04 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
关于JavaScript的一些看法
2009/05/27 Javascript
javascript自启动函数的问题探讨
2013/10/05 Javascript
JS判断对象是否存在的10种方法总结
2013/12/23 Javascript
jquery实现列表上下移动功能
2016/02/25 Javascript
js实现div在页面拖动效果
2016/05/04 Javascript
angular.js指令中transclude选项及ng-transclude指令详解
2017/05/24 Javascript
基于 flexible 的 Vue 组件:Toast -- 显示框效果
2017/12/26 Javascript
Es6 Generator函数详细解析
2018/02/24 Javascript
vue改变循环遍历后的数据实例
2019/11/07 Javascript
[19:14]DOTA2 HEROS教学视频教你分分钟做大人-维萨吉
2014/06/24 DOTA
[00:14]护身甲盾
2019/03/06 DOTA
Python操作Access数据库基本步骤分析
2016/09/19 Python
python中Pycharm 输出中文或打印中文乱码现象的解决办法
2017/06/16 Python
python3使用pyqt5制作一个超简单浏览器的实例
2017/10/19 Python
Python设计模式之抽象工厂模式原理与用法详解
2019/01/15 Python
python数据挖掘需要学的内容
2019/06/23 Python
Django 权限认证(根据不同的用户,设置不同的显示和访问权限)
2019/07/24 Python
opencv-python 提取sift特征并匹配的实例
2019/12/09 Python
理解Django 中Call Stack机制的小Demo
2020/09/01 Python
美国知名的时尚购物网站:Anthropologie
2016/12/22 全球购物
丝芙兰波兰:Sephora.pl
2018/03/25 全球购物
土木工程个人自荐信范文
2013/11/30 职场文书
马云的职业生涯规划之路
2014/01/01 职场文书
六个一活动实施方案
2014/03/21 职场文书
竞选宣传委员演讲稿
2014/05/24 职场文书
经典毕业生求职信
2014/07/12 职场文书
租房协议书
2014/09/12 职场文书
护士2014年终工作总结
2014/11/11 职场文书
2014年护理部工作总结
2014/11/14 职场文书
MySQL优化之如何写出高质量sql语句
2021/05/17 MySQL
python随机打印成绩排名表
2021/06/23 Python
Redis特殊数据类型bitmap位图
2022/06/01 Redis