Takeaway: This document outlines the more useful functions available in the PHP ctype extension, with explanations and usage examples.
Almost every PHP application, be it a Web form or a CLI program, requires user input. And because users are notoriously fickle, it's important to check the data they enter for validity before using it for anything important—for instance, a calculation or a database entry.
Fortunately, there's a hidden jewel in the PHP toolkit that can help with this task. It's the PHP ctype extension, and it contains functions to test if a value belongs to a particular character class—that is, whether it contains only alphabetic characters, only numbers, only printable or special characters, or combinations thereof.
This document outlines the more useful functions available in this PHP extension, with explanations and usage examples. (Table A)
Table A
Function
Explanation
Example
ctype_alpha($val)
This function returns true if every character in the input string is from the set of alphabetic characters.
Use this function for input that must contain alphabetic characters—for example, personal names.
ctype_digit($val)
This function returns true if every character in the input string is a number.
Use this function for input that must only contain numbers—for example, age values or credit card numbers.
Note: Decimal points in floating-point numbers will cause this function to return false.
ctype_alnum($val)
This function returns true if every character in the input string is either a number or an alphabetic character.
Note: White space or punctuation in the input string will cause this function to return false.
ctype_space($val)
This function returns true if every character in the input string "creates whites pace"—that is, it is a space, tab, line feed or carriage return character.
ctype_cntrl($val)
This function returns true if every character in the input string is a control character, such as a tab or carriage return.
Note: White space in the input string will cause this function to return false.
ctype_punct($val)
This function returns true if every character in the input string is a punctuation character—that is, not an alphabetic, numeric, control or white space character.
ctype_print($val)
This function returns true if every character in the input string is printable—that is, an alphabetic, numeric, punctuation or white space character. The presence of control characters, such as line feeds or tabs, will cause this function to return false.
ctype_graph($val)
This function returns true if every character in the input string is printable and not white space—that is, an alphabetic, numeric or punctuation character. The presence of white space or control characters, such as line feeds or tabs, will cause this function to return false.
ctype_upper($val)
and
ctype_lower($val)
These functions return true if every character in the input string is an alphabetic character and either upper-case or lower-case respectively.
Almost every PHP application, be it a Web form or a CLI program, requires user input. And because users are notoriously fickle, it's important to check the data they enter for validity before using it for anything important—for instance, a calculation or a database entry.
Fortunately, there's a hidden jewel in the PHP toolkit that can help with this task. It's the PHP ctype extension, and it contains functions to test if a value belongs to a particular character class—that is, whether it contains only alphabetic characters, only numbers, only printable or special characters, or combinations thereof.
This document outlines the more useful functions available in this PHP extension, with explanations and usage examples. (Table A)
Table A
Function
Explanation
Example
ctype_alpha($val)
This function returns true if every character in the input string is from the set of alphabetic characters.
Use this function for input that must contain alphabetic characters—for example, personal names.
ctype_digit($val)
This function returns true if every character in the input string is a number.
Use this function for input that must only contain numbers—for example, age values or credit card numbers.
Note: Decimal points in floating-point numbers will cause this function to return false.
ctype_alnum($val)
This function returns true if every character in the input string is either a number or an alphabetic character.
Note: White space or punctuation in the input string will cause this function to return false.
ctype_space($val)
This function returns true if every character in the input string "creates whites pace"—that is, it is a space, tab, line feed or carriage return character.
ctype_cntrl($val)
This function returns true if every character in the input string is a control character, such as a tab or carriage return.
Note: White space in the input string will cause this function to return false.
ctype_punct($val)
This function returns true if every character in the input string is a punctuation character—that is, not an alphabetic, numeric, control or white space character.
ctype_print($val)
This function returns true if every character in the input string is printable—that is, an alphabetic, numeric, punctuation or white space character. The presence of control characters, such as line feeds or tabs, will cause this function to return false.
ctype_graph($val)
This function returns true if every character in the input string is printable and not white space—that is, an alphabetic, numeric or punctuation character. The presence of white space or control characters, such as line feeds or tabs, will cause this function to return false.
ctype_upper($val)
and
ctype_lower($val)
These functions return true if every character in the input string is an alphabetic character and either upper-case or lower-case respectively.
No comments:
Post a Comment