Fri, 17 May 2024


PHP Character Function

By: Peter, NetArt Media
Wed, 1 August 2018
PHP Character Function

Characters, they make up the gist of what we see on the internet. Whether they are of the alphanumeric, numeric or printable variety, it's certainly helpful to have a function within PHP that allows for the ability to manipulate and return them according to our particular desires.

There are several functions that make up the PHP 'character' series, but I will cover 7 of them today. I use most of these regularly, whenever situations call for them.

For this function, we use the 'ctype' clause, where each particular function name is appended after it with an underscore in-between.

Alphanumeric Characters - ctype_alnum



The result of the above code block will be...

1890ABCD contains only letters and digits.
DCBA0981# does not contain only letters and digits.

This would make sense, as the '#' is not alphanumeric. As you can see, this is self-explanatory function. Using arrays is a good idea when searching for particular characters, as is creating conditions for getting particular returns. I use both in all the examples below.

Alpha Characters - ctype_alpha


This function will return letters.


The result...

ABCDefgh contains only letters.
DCBA0981 does not contain only letters.

Control Characters - ctype_cntrl


This function will return control characters if they are found within the array. That won't happen very often, as most characters returned on a web page in a browser are printable and don't offer much in the way of control. This function could be used more for content management systems, as opposed to the creation of website pages.


The result...

U+0000 does not contain control characters.
carriagen does not contain control characters.

Digit Characters - ctype_digit


Returns strings which contain digits. Take note that this function does not recognize decimals.


The result...

10800 contains all digits.
10800.67 does not contain all digits.

Printable Characters - ctype_print


It's great to be able to locate printable characters. This function can be used for safety measures, as well as making for a better user experience for visitors to a website. 'ctype_graph' can also be used, however, 'print' recognizes whitespace as being printable. Thus, making it easier to use.


The result...

^7Up# contains visible printable characters.
I am printable. contains visible printable characters.

Lowercase Characters - ctype_lower


Find lowercase characters and return accordingly.


The result...

UPPERCASE does not contain lower case letters.
lowercase contains all lower case letters.

Uppercase Characters - ctype_upper


Find uppercase characters and return accordingly.


The result...

UPPERCASE contains all upper case letters.
lowercase does not contain upper case letters.

As you can probably see, these examples could be taken to much higher levels. While and for loops could be used, as well as more in-depth conditional statements, providing much in the way of furthering better experiences for both back and front end users. I highly suggest you do more research on the PHP 'character' function and see what you can come up with.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox