Alpha Numeric PHP Check

An alphanumeric value is a string that contains some combination of either alphabetic or numeric characters and nothing else. For example, the word "hello" is alphanumeric, as is the number "43" and string value "1stplace" is too, but "hello!" and "1st place" are not, the former because it contains a punctuation mark and the latter because it has a space. PHP has a function called "ctype_alnum" that checks strings for alphanumeric characters.

  1. Uses

    • You use the ctype_alnum function to check strings for specific characters or in combination with another function such as str_replace to filter them for data content. For example, you can use the str_replace function to remove certain characters from a string, such as spaces and periods, then check the remaining characters in the ctype_alnum function. If the function returns true, then the string may contain only alphanumeric values, spaces and periods with no additional characters.

    Parameters

    • The ctype_alnum function takes only one parameter: a text value that contains the string to check for alphanumeric characters. You have the option to pass a PHP variable with a string value, directly use a string within single or double quotation marks, or even pass functions that return string values. The function does not permit you to use zero parameters, nor can you overload the function by passing any optional extra parameters. If you do, PHP returns an error.

    Syntax

    • To use the function, open a PHP tag in your HTML document and type the function name followed by brackets that contain the text value. For example, type "ctype_alnum($mystringvariable);" or "ctype_alnum('mystringliteral');" or even "ctype_alnum(mystringfunction());" to call the ctype_alnum function using a variable, string literal or function call, respectively. You can also use it within other blocks of codes such as loops or conditional statements to help choose an outcome based on the Boolean value that the function returns.

    Return Values

    • When you pass a string to the ctype_alnum function, it returns with one of two possible values: true if all of the characters in the string are alphanumeric characters, or false if it has at least one non-alphanumeric character. If you echo the result of the function call to the screen, the function displays the number one for true or nothing for false. However, you can use an if statement to choose what to display with regard to the function call's result.

Related Searches:

References

Comments

Related Ads

Featured