博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ctype函数_Ctype函数简介
阅读量:2508 次
发布时间:2019-05-11

本文共 8048 字,大约阅读时间需要 26 分钟。

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); // truectype_digit($integer);        // false (ASCII 42 is '*')is_numeric($numeric_string);  // trueis_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()函数。 它只将数字视为数字,然后将其返回真值,如下所示:

<?phpis_numeric($numeric_string);  // trueis_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:

这是一个例子:

<?phpif (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);         // falsectype_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

图片来自

翻译自:

ctype函数

转载地址:http://gorgb.baihongyu.com/

你可能感兴趣的文章
一个宏定义引发的问题
查看>>
[oracle实验]跨平台传输表空间 win -> linux
查看>>
HDU1548 A strange lift BFS 简单题
查看>>
tcp 的6个控制位
查看>>
Web2.0寒流
查看>>
九度OJ 1122:吃糖果
查看>>
js的JSON对象
查看>>
做北京项目的过程所得
查看>>
PHP模拟post提交数据方法汇总
查看>>
windows环境libevent搭建和demo分析
查看>>
Javascript通过bind()掌控this
查看>>
JavaWeb_JSTL标签数据的存储
查看>>
linux 常见问题
查看>>
Android相机实时自动对焦的完美实现
查看>>
OpenCV入门笔记(七) 文字区域的提取
查看>>
flask中的上下文处理器context_processor
查看>>
项目中的一个分页功能pagination
查看>>
[GraphQL] Use Arguments in a GraphQL Query
查看>>
[React] Pass Data To Event Handlers with Partial Function Application
查看>>
SEO怎么去发布外链?
查看>>