PHP ctype_print() function with example

PHP ctype_print() work: Here, we will find out about the ctype_print() work with the example in PHP.

PHP ctype_print() work

ctype_print() work is a character type (CType) work in PHP, it is utilized to check whether a given string contains every printable character or not.

It returns genuine if all characters of the given strings are printable. Else it returns false.

Printable characters are the characters that can be imprinted on the screen. The all letters in order, digits, whitespace and unique characters are the printable characters.

Syntax:

    ctype_print(string) : bool

Example:

    Input: "Hello 123"
    Output: true
    Input: "I am 123@#!~*.()"
    Output: true
    Input: "Hello\r\nWorld"
    Output: false
    Input: "Hello\x41"
    Output: true

Output:

<?php
    $str = "Hello 123";
    if(ctype_print($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "I am 123@#!~*.()";
    if(ctype_print($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello\r\nWorld"; //"\r" and "\n" are not printable characters
    if(ctype_print($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");

    $str = "Hello\x41"; //"\x41" is the ASCII value of "A"
    if(ctype_print($str))
        echo ("$str contains all printable characters.\n");
    else
        echo ("$str does not contain all printable characters.\n");	
?>

Output:

Hello 123 contains all printable characters.
I am 123@#!~*.() contains all printable characters.
Hello
World does not contain all printable characters.
HelloA contains all printable characters.

Leave a Comment

error: Alert: Content is protected!!