PHP - Function ctype_print()



The PHP Character type checking ctype_print() function checks if a string contains only printable characters, which include letters, numbers, punctuation, and white space. Non-printable characters like "\n", "\t", and "\r" are not considered printable.

This function returns a boolean value true if the string consists only of printable characters; otherwise, it returns false. If the string is empty (""), the function also returns false.

Syntax

Following is the syntax of the PHP Character type checking ctype_print() function −

ctype_print(mixed $text): bool

Parameters

This function accepts the following parameters −

  • text (required) − The string needs to be tested.

Return Value

This function returns "true" if every character in the text will actually create output (including blanks); and "false" otherwise.

Example 1

The following is the basic example of the PHP ctype_print() function −

<?php
   $string = "Tutorialspoint\n\r";
   echo "The given string is: $string";
   echo "\nDoes the '$string' consist of all the printable characters? ";
   var_dump(ctype_print($string));
?>

Output

The above program produces the following output −

The given string is: Tutorialspoint

Does the 'Tutorialspoint
' consist of all the printable characters? bool(false)

Example 2

If the given string (text) does not contain all the printable characters, the PHP ctype_print() function will return false

<?php
   $string = "Tutorialspoint1332";
   echo "The given string is: $string";
   echo "\nDoes the '$string' consist of all the printable characters? ";
   var_dump(ctype_print($string));
?>

Output

After executing the above program, the following output will be displayed −

The given string is: Tutorialspoint1332
Does the 'Tutorialspoint1332' consist of all the printable characters? bool(true)

Example 3

If the given string is empty (""), this function always returns false

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the '$string' consist of all the printable characters? ";
   var_dump(ctype_print($string));
?>

Output

Once the above program is executed, it generates the following output −

The given string is:
Does the '' consist of all the printable characters? bool(false)

Example 4

In the example below, we create an array of strings containing multiple string values, and using the PHP ctype_print() function within the for-each loop check whether each string consists of printable characters −

<?php
   $strings = array("asdf\n\r\t", "hello123", 'fooo#int%@');
   echo "The given strings are: ";
   foreach ($strings as $text){
	   echo $text." ";
   }
   foreach ($strings as $test) {
      if (ctype_print($test)) {
         echo "\nThe string '$test' consists of all the printable characters.";
      }else {
         echo "\nThe string '$test' does not consist of all the printable characters.";
      }
   }
?>

Output

Following is the output of the above program −

The given strings are: asdf
         hello123 fooo#int%@
The string 'asdf
        ' does not consist of all the printable characters.
The string 'hello123' consists of all the printable characters.
The string 'fooo#int%@' consists of all the printable characters.
php_function_reference.htm
Advertisements