ctype函数

The Ctype extension provides a set of functions that are used to verify whether the characters in a string are of the correct type. In this article we’ll take a look at the syntax used by the character type functions, see what specific functions exist, and how they are used to perform validation.

Ctype扩展提供了一组函数,这些函数用于验证字符串中的字符是否为正确的类型。 在本文中,我们将研究字符类型函数使用的语法,查看存在哪些特定函数以及如何使用它们执行验证。

The extension is enabled by default if you’re running PHP 4.2 or above. If you cannot stand the thought of this extension running with your installation for some crazy reason, then you can use the --disable-ctype compile switch to turn it off.

如果您运行的是PHP 4.2或更高版本,则默认情况下启用该扩展。 如果出于某种疯狂的原因无法忍受此扩展随安装一起运行,则可以使用--disable-ctype编译开关将其关闭。

If you have a background in C, then you’re probably already familiar with the character type functions because that is where they come from (don’t forget that PHP is actually written in C). But if you’re into Python, then it’s only fair to point out that the PHP Ctype functions have absolutely nothing to do with the Python’s ctypes library. It’s just one of those tragic and totally unavoidable naming similarities.

如果您具有C的背景知识,那么您可能已经熟悉字符类型函数,因为这就是它们的来源(别忘了PHP实际上是用C编写的)。 但是,如果您对Python感兴趣,那么仅指出PHP Ctype函数与Python的ctypes库完全无关即可。 这只是悲剧性的,完全不可避免的命名相似性之一。

那么它是怎样工作的? (So How Does it Work?)

It’s all very simple. As I noted before, the functions check a string value to see if the characters are within a given range or that every character is of the appropriate type. For example, you can use these functions to see if a string contains only uppercase characters, or if it’s numeric, or if it consists of hex characters, or one of the other dozen or so options available.

一切都非常简单。 如前所述,这些函数检查字符串值,以查看字符是否在给定范围内,或者每个字符是否具有适当的类型。 例如,您可以使用这些函数查看字符串是否仅包含大写字符,数字或数字,十六进制字符或其他可用的大约十个选项之一。

You should be diligent in making sure you always pass in a string. Sure, you can pass in integers, but you’re setting yourself up for trouble as the PHP manual notes on every function’s page:

您应该努力确保始终输入一个字符串。 当然,您可以传入整数,但是您会遇到麻烦,因为PHP手册在每个函数的页面上都提到了这一点:

If an integer between -128 and 255 inclusive is provided, it is interpreted as the ASCII value of a single character (negative values have 256 added in order to allow characters in the Extended ASCII range). Any other integer is interpreted as a string containing the decimal digits of the integer.

如果提供的整数在-128到255之间(含-128和255),则将其解释为单个字符的ASCII值(负值添加256以允许扩展ASCII范围内的字符)。 其他任何整数都解释为包含该整数的十进制数字的字符串。

This example from the ctype_digit() page illustrates the effect of this:

ctype_digit()页面上的此示例说明了此操作的效果:

<?php
$numeric_string = "42";
$integer        = 42;
ctype_digit($numeric_string); // true
ctype_digit($integer);        // false (ASCII 42 is '*')
is_numeric($numeric_string);  // true
is_numeric($integer);         // true

If we look at the code above, the first line evaluates as true. The second statement, however, is false. It is true that 42 is an integer, but the ctype statement evaluates it as a single ASCII character which in this case is an asterisk.

如果我们看上面的代码,第一行的评估结果为true。 但是,第二个陈述是错误的。 的确,42是整数,但是ctype语句将其评估为单个ASCII字符,在这种情况下为星号。

Ctype functions are not the only way to validate data, of course. You can also use the is_numeric() function, depending on your needs. It treats a numeric as just that, a number, and returns true value as shown below:

当然,Ctype函数不是验证数据的唯一方法。 您还可以根据需要使用is_numeric()函数。 它只将数字视为数字,然后将其返回真值,如下所示:

<?php
is_numeric($numeric_string);  // true
is_numeric($integer);         // true

There are also other is_* functions, including is_float(), is_integer(), etc.

还有其他is_*函数,包括is_float()is_integer()等。

Why are we talking about is_* functions here? Just to remind you there is more than one way to skin a cat. Actually, in today’s age I’m probably not supposed to say that. It’s just an expression. Don’t skin your cat and then tell everyone it was my idea. It’s just that there are multiple ways of doing things.

我们为什么在这里谈论is_*函数? 只是要提醒您,有一种不只一种给猫皮的方法。 实际上,在当今时代,我可能不应该这么说。 这只是一种表达。 不要给猫咪剥皮,然后告诉所有人这是我的主意。 只是有多种做事方式。

可用功能 (What Functions are Available)

I’ve hinted that a wide range of checking is available, but what exactly are the functions available? What kind of checking is available? The list is below.

我暗示了可以进行广泛的检查,但是可用的功能到底是什么? 可以进行哪种检查? 列表如下。

  • ctype_alnum() – check for alphanumeric characters (A – Z, upper or lower case, 0-9, no special characters, punctuation, or other freaks).

    ctype_alnum() –检查字母数字字符(A – Z,大写或小写,0-9,无特殊字符,标点符号或其他怪胎)。

  • ctype_alpha() – check for alphabetical characters (A-Z, upper or lower case).

    ctype_alpha() –检查字母字符(AZ,大写或小写)。

  • ctype_cntrl() – check for control characters (things like n, etc.).

    ctype_cntrl() –检查控制字符(诸如n东西)。

  • ctype_digit() – check for numeric characters (0-9, no decimal point, comma, etc.).

    ctype_digit() –检查数字字符(0-9,无小数点,逗号等)。

  • ctype_graph() – check for visually printable characters (not control characters or space).

    ctype_graph() –检查视觉上可打印的字符(非控制字符或空格)。

  • ctype_lower() – check for lowercase characters (a-z lower case only, no numbers).

    ctype_lower() –检查小写字符(仅az小写,无数字)。

  • ctype_print() – check for printable characters including control characters and space.

    ctype_print() –检查可打印字符,包括控制字符和空格。

  • ctype_punct() – check for punctuation type characters (no numbers, letters, or space). Generally these include many of the “swear word” characters that we often call “special” characters. @&!#

    ctype_punct() –检查标点类型字符(无数字,字母或空格)。 通常,这些字符包括许多我们经常称为“特殊”字符的“脏话”字符。 @&!#

  • ctype_space() – check for whitespace characters (includes blanks and any control character that leaves a space on the page, such as “Narrow No-Break Space” and the “Mongolian Vowel Separator”).

    ctype_space() –检查空格字符(包括空格和在页面上留下空格的任何控制字符,例如“窄无间断空格”和“蒙古语元音分隔符”)。

  • ctype_upper() – check for uppercase characters (A-Z upper case only, no numbers or special characters).

    ctype_upper() –检查大写字符(仅AZ大写,没有数字或特殊字符)。

  • ctype_xdigit() – check for hex characters.

    ctype_xdigit() –检查十六进制字符。

如何使用它们 (How to Use Them)

Using the Ctype functions is pretty easy. You just set it up, generally, in an if-statement, sometimes embedded in a loop if you’re testing a number of values from in an array, and then check if the result of the function call is true or false. True means that every character in that string is the type of character called for by that specific function.

使用Ctype函数非常简单。 通常,您只是在if语句中进行设置,如果要在数组中测试多个值,有时会嵌入到循环中,然后检查函数调用的结果是true还是false。 True表示该字符串中的每个字符都是该特定函数所要求的字符类型。

Here’s an example:

这是一个例子:

<?php
if (ctype_alnum($string)) {
echo "This string totally works";
}
else {
echo "And this one not so much";
}

If the value of $string is “Azxc1234” then you’ll see the message that it works. If $string is “123#Axy” then it won’t because # is not an alphanumeric character.

如果$string值为“ Azxc1234”,则您将看到它起作用的消息。 如果$string为“ 123#Axy”,则不会,因为#不是字母数字字符。

Note that if you pass in an empty string, the functions will false in PHP 5.1 and above, but true if you are on an earlier version (just another reason to upgrade now!).

请注意,如果传入空字符串,则函数在PHP 5.1及更高版本中将为false,但在较早版本中为true(这是立即升级的另一个原因!)。

And also remember to make sure the input to a Ctype function is a string! When in doubt, there’s no harm in casting it.

还要记住确保Ctype函数的输入是字符串! 如有疑问,投射它不会有任何危害。

<?php
$integer = 42;
ctype_digit($integer);         // false
ctype_digit((string)$integer); // true

结论 (Conclusion)

And that’s all there is to it! The functions should inherently be part of your PHP installation (if not then you definitely need to upgrade or stop setting up weird PHP set ups). And they are simple to use so long as you only input strings.

这就是全部! 这些功能本来应该是PHP安装的一部分(如果不是,那么您肯定需要升级或停止建立怪异PHP设置)。 只要您只输入字符串,它们就易于使用。

So where would you use them? Well, anytime you need to bring a string in from a form you could use them to test the validity of the data you are dealing with. Really, the sky’s the limit. (Fortunately, I don’t have to take a public or private stand on what you use them for; I just say what the functions are and how to use them).

那么您将在哪里使用它们? 好了,只要您需要从表单中输入字符串,就可以使用它们来测试所处理数据的有效性。 真的,天空是极限。 (幸运的是,我不必针对您使用它们的用途采取公开或私人立场;我只是说说这些功能是什么以及如何使用它们)。

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/an-introduction-to-ctype-functions/

ctype函数

ctype函数_Ctype函数简介相关推荐

  1. 关于c语言字符串函数和一些内存函数的的简介

    关于c语言字符串函数和一些内存函数的的简介 求字符串长度的函数 strlen函数介绍![在这里插入图片描述](https://img-blog.csdnimg.cn/20190301142458376 ...

  2. eval函数 php_PHP的一句话木马代码和函数eval的简介

    大清早的刚从床上爬起来.雨落就跑来找我问我这段代码是什么意思<?php @eval($_POST[pp]);?>看了一下,post接收pp的值,抑制错误输出.呵呵开个玩笑,其实不是这么简单 ...

  3. sklearn之pipeline:pipeline函数/make_pipeline函数的简介及其区别联系、使用技巧、案例应用之详细攻略

    sklearn之pipeline:pipeline函数/make_pipeline函数的简介及其区别联系.使用技巧.案例应用之详细攻略 目录 sklearn.pipeline函数简介 1.Why pi ...

  4. 【Android UI】Path 测量 PathMeasure ② ( PathMeasure API 简介 | nextContour 函数 | getPosTan 函数 ★ | 曲线切线处理 )

    文章目录 一.PathMeasure API 简介 1.nextContour 函数 2.getPosTan 函数 ★ 一.PathMeasure API 简介 PathMeasure 官方文档 : ...

  5. c语言qsort函数简介,C语言排序函数—qsort函数

    前言: 在一些编程题中经常需要你按照某个指标按照从小到大或从大到小输出一些数据,这时你可以自己写一个排序函数进行排序,但是其实C语言函数库中就有一个排序函数--qsort函数,使用它可以节省你单独写排 ...

  6. strcpy_s函数/strcpy函数简介

    strcpy /* *描述:此类函数是用于对字符串进行复制(拷贝). * *参数: * [in] strSource:需要拷贝的字符串 * [out] strDestination:拷贝完成之后的字符 ...

  7. python nums函数_Python函数

    一.简介 函数是可重用的程序代码块.函数的作用,不仅可以实现代码的复用,更能实现代码的一致性.一致性指的是,只要修改函数的代码,则所有调用该函数的地方都能得到体现. 函数用关键字def来定义,def关 ...

  8. 【Flutter】Dart 函数 ( 函数构成 | 私有函数 | 匿名函数 | 函数总结 )

    文章目录 一.Dart 函数构成 二.Dart 私有函数 三.Dart 匿名函数 四.完整代码示例 五.Dart 方法总结 六. 相关资源 一.Dart 函数构成 Dart 函数构成 : 方法名 , ...

  9. C语言signal()函数(通过设置一个函数(回调函数)来处理捕获到异常信号时需要执行的操作)

    文章目录 描述 声明 参数 返回值 实例 附加解释 背景知识:C语言中signal函数简介及使用 描述 C 库函数 void (*signal(int sig, void (*func)(int))) ...

  10. if函数python_pythonif函数

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! python的内置的函数round() a = 1.12586 #保留小数的函数 ...

最新文章

  1. JAVA垃圾回收机制-史上最容易理解看这一篇就够了
  2. 史上最全最强SpringMVC详细示例实战教程
  3. Docker(二)安装及常用命令
  4. 分分合合分分,谷歌医疗走向大败退
  5. Matlab形态学图像处理:二值图像分割 标记连通区域和重心位置 删除连通区域
  6. Python之数据分析(卷积运算、移动均线、布林带)
  7. seL4操作系统基础06:dataport interface与seL4SharedData connector
  8. MIT大牛Gilbert Strang新书:《线性代数与从数据中学习》抢先看
  9. javascript 模仿 html5 placeholder
  10. java gbk文件转utf8_java 将GBK编码文件转为UTF-8编码
  11. 我在用的多功能电子书阅读器
  12. oracle数据库编码
  13. PowerShell、CMD 和 Windows Terminal 的美化配置方法
  14. selenium 接管浏览器
  15. 应届生参加工作,什么事情越早知道越好?
  16. 【蓝桥杯】 《3W字数总结》 蓝桥杯Java必备基础知识以及国赛真题解析
  17. satoken+ gateway网关统一鉴权 初版
  18. VC6link卡死解决办法
  19. 物联网平台如何切入智慧校园
  20. 医学图像分割新网络:Boundary-aware Context Neural Network for Medical Image Segmentation

热门文章

  1. 利用cropper插件进行图片裁剪
  2. python编程练习:爬虫爬取全国大江大河实时水情
  3. mysql ipv4转ipv6_从IPv4到IPv6过渡
  4. 总线及单片机各种线 以及数据总线宽度
  5. Android sn 号修改
  6. ai训练 样本大小_成本低、周期短,小样本学习能否成为AI落地的速效药?
  7. java speex回声消除_android 利用speex 音频降噪,回声消除demo
  8. xmapp启动数据库问题记录 Access denied for user ‘me‘@‘localhost‘ (using password: NO)
  9. 是否还为制作PPT而烦恼?珍藏的PPT实用网站打包送给你!
  10. 【好看图标不用愁】吾爱万能软件ICO图标提取器