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 相关文章推荐
PHP3 safe_mode 失效漏洞
Oct 09 PHP
简单的php 验证图片生成函数
May 21 PHP
php面向对象全攻略 (十四) php5接口技术
Sep 30 PHP
php 需要掌握的东西 不做浮躁的人
Dec 28 PHP
php中将数组存到文件里的实现代码
Jan 19 PHP
浅析PHP编程中10个最常见的错误
Aug 08 PHP
PHP生成条形码大揭秘
Sep 24 PHP
SSO单点登录的PHP实现方法(Laravel框架)
Mar 23 PHP
php简单中奖算法(实例)
Aug 15 PHP
PHP实现超简单的SSL加密解密、验证及签名的方法示例
Aug 28 PHP
php加速缓存器opcache,apc,xcache,eAccelerator原理与配置方法实例分析
Mar 02 PHP
Laravel框架源码解析之入口文件原理分析
May 14 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笔记之常用文件操作
2010/10/12 PHP
PHP合并静态文件详解
2014/11/14 PHP
php实现微信模板消息推送
2018/03/30 PHP
PHP连接SQL Server的方法分析【基于thinkPHP5.1框架】
2019/05/06 PHP
基于jquery的模态div层弹出效果
2010/08/21 Javascript
js输入框邮箱自动提示功能代码实现
2013/12/10 Javascript
JS数组的常见用法实例
2015/02/10 Javascript
以Python代码实例展示kNN算法的实际运用
2015/10/26 Javascript
jquery+css3实现会动的小圆圈效果
2016/01/27 Javascript
很棒的js Tab选项卡切换效果
2016/08/30 Javascript
jQuery子元素过滤选择器用法示例
2016/09/09 Javascript
Bootstrap简单表单显示学习笔记
2016/11/15 Javascript
详解Angular CLI + Electron 开发环境搭建
2017/07/20 Javascript
JavaScript之创意时钟项目(实例讲解)
2017/10/23 Javascript
Vue中的Vux配置指南
2017/12/08 Javascript
详解webpack babel的配置
2018/01/09 Javascript
vuejs点击class变化的实例
2018/09/05 Javascript
详解puppeteer使用代理
2018/12/27 Javascript
在vue中使用防抖和节流,防止重复点击或重复上拉加载实例
2019/11/13 Javascript
vue 判断元素内容是否超过宽度的方式
2020/07/29 Javascript
vue中watch的用法汇总
2020/12/28 Vue.js
用python求一个数组的和与平均值的实现方法
2019/06/29 Python
kali中python版本的切换方法
2019/07/11 Python
pytorch实现用Resnet提取特征并保存为txt文件的方法
2019/08/20 Python
python安装virtualenv虚拟环境步骤图文详解
2019/09/18 Python
python实现门限回归方式
2020/02/29 Python
使用Python发现隐藏的wifi
2020/03/04 Python
python实现简单学生信息管理系统
2020/04/09 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
css3使网页、图片变成灰色兼容大多数浏览器
2014/07/02 HTML / CSS
html5实现滑块功能之type=&quot;range&quot;属性
2020/02/18 HTML / CSS
美国领先的家庭智能音响系统品牌:Sonos
2018/07/20 全球购物
初中作文评语集锦
2014/12/25 职场文书
2015年政务公开工作总结
2015/05/19 职场文书
德劲DE1105机评
2022/04/05 无线电
java如何实现获取客户端ip地址的示例代码
2022/04/07 Java/Android