26 Votes

PHP: Check Strings with Ctype-Functions for Character Classes

Article by Stefan Trost | Last update on 2021-05-10 | Created on 2012-06-16

If you want to test the characters of a string and check whether these consist only of a certain class of characters (letters, digits, etc), you will mostly use regular expressions.

However, regular expressions are in several respects not an ideal solution. First, they require a lot of effort in programming. They are bulky in the code and quickly it comes to a mistake. Secondly, the performance of such functions is not very good, since some steps are necessary to interpret a code using a regular expression and the interpreter cannot apply native implementations all the way.

Especially for the most common tests as only letters, only numbers, or only numbers and letters, a number of implemented PHP functions make sense, that I would like to introduce in this article, and that are still relatively rare applied in practice.

We are talking about the Ctype functions that were directly written and optimized in C, and therefore they are providing a lot more better performance than all other solutions like regular expressions and all forms of is_int(), is_string() and so on.

In the following, I want to present you all ctype functions and the character classes, for which they can be used. It is worth mentioning here, that you can change the described classes via the locale settings with the function setlocale(). But that would go at this point too far.

Check for Letters (ctype_alpha)

if (ctype_alpha($string)) { echo 'Only Letters'; }
ctype_alpha('AzUiL') //true
ctype_alpha('A bCX') //false

The function ctype_alpha() tests whether the given string consists only of letters. It returns true if the string contains only lowercase or uppercase letters from A to Z. Once a character is not a small or capital letter of this range, ctype_alpha() returns false.

Check for Digits (ctype_digit)

if (ctype_digit($string)) { echo 'Only  Digits'; }
ctype_digit('12345') //true
ctype_dight('12,45') //false
ctype_digit('ABC12') //false

The function ctype_digit() is only true if all characters of the provided string are only digits. Once a character is not a digit, it is false. Accordingly even a string like "1.2" or "1,2" returns false.

Check for Letters and Numbers (ctype_alnum)

if (ctype_alnum($string)) { echo 'Only Digits or Letters'; }
ctype_alnum('12345') //true
ctype_alnum('AzazO') //true
ctype_alnum('12AT9') //true
ctype_alnum('su.78') //false

The function ctype_alnum() returns true if the string is only made of the characters A to Z, a to z and 0 to 9, so it may occur digits and letters. Once a character is not falling into this class, it returns false.

Lowercase and Uppercase Letters (ctype_lower/upper)

if (ctype_lower($string)) { echo 'Only Lowercase'; }
if (ctype_upper($string)) { echo 'Only Uppercase'; }
ctype_lower('AzAzU') //false
ctype_lower('azazu') //true
ctype_upper('ABCD8') //false
ctype_upper('IUAOP') //true

If you want to check, whether a string contains only the lowercase letters a to z, you can use the function ctype_lower(). The function ctpye_upper() can be used to check, whether a string consists only of the uppercase letters A to Z.

Check for Printable Characters

if (ctype_graph($string)) { echo 'Printable, but not a space'; }
if (ctype_print($string)) { echo 'Printable, no spaces'; }
ctype_graph('abc123#+*-.!')  //true
ctype_graph('abc\n\r\t')     //false
ctype_graph('abc abc 123')   //false
ctype_print('abc123#+*-.!')  //true
ctype_print('abc\n\r\t')     //false
ctype_print('abc abc 123')   //true

With the help of the functions ctype_graph() and ctype_print(), you can determine whether a string contains only printable characters. Printable characters are characters that actually cause an output, so for example letters, numbers, special characters such as visible dots, exclamation points, and so on. False is returned, if at least one of the characters is not visible, such as control characters, line breaks and tabs.

The difference between ctype_graph() and ctype_print() is how to deal with spaces. In the case of ctype_graph(), false is returned when spaces appear in the string, in the case of ctype_print() true is returned when there are spaces in the string and all other characters are printable.

Whitespaces, Special Characters and Control Characters

if (ctype_cntrl($string)) { echo 'Only control characters'; }
if (ctype_punct($string)) { echo 'No letters / numbers / spaces'; }
if (ctype_space($string)) { echo 'Only whitespace'; }
ctype_cntrl('abc\n123')  //false
ctype_cntrl('\n\r\t')    //true
ctype_punct('abcdef!')   //false
ctype_punct('#+*-.!')    //true
ctype_space('     ')     //true
ctype_space('/n/t')      //true
ctype_space('abc/nab 1') //false

The function ctype_cntrl() checks whether the string only consists of control characters. It returns true if only tabs, line breaks, and other control characters are included in the string. If other characters occur in the string, it returns false.

With ctype_punct(), it can be verified that the string contains only characters such as dots, brackets, exclamation marks and so on, which you can print. It returns false, if the string contains letters, numbers, spaces, or non-printable characters such as line breaks and similar characters.

The function ctype_space() can be used to test a string, whether it consists of characters that create some form of space. These are, for example, tabs, normale spaces and line breaks.

Check for Hexadecimal Digits (ctype_xdigit)

if (ctype_xdigit($string)) { echo 'Is Hexadecimal Digit'; }
ctype_xdigit('A09BC81')  //true
ctype_xdigit('af179d')   //true
ctype_xdigit('AP12222')  //false

The function ctype_xdigit() can check a string to see whether it is a hexadecimal digit. Such a figure can only include the digits 0 to 9 and the letters A to F. It makes sense to use ctype_digit(), for example, to check whether a user input is really a color value in hexadecimal notation.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

HTACCESS: Simplify URL

Tutorial | 0 Comments

PHP: Current Date and Time

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.