ctype_print
Checks whether every character in a string is printable, including the space character.
ctype_print(mixed $text): bool- Added in PHP 4.0.4
Parameters
| Parameter | Type | Description |
|---|---|---|
$text | mixed | The string to test. Every byte must be printable for the result to be true. An int between -128 and 255 is interpreted as an ASCII codepoint, which is why non-string arguments are deprecated as of PHP 8.1. |
Return value
booltrue if every character in text produces output, including blanks; false if text contains control characters or characters with no output at all. An empty string always returns false.
Errors
Never throws. A non-string argument raises an E_DEPRECATED notice as of PHP 8.1; values that are neither string nor int return false.
Changelog
- PHP 8.1Passing a non-string argument is deprecated. Ints between -128 and 255 are still read as ASCII codepoints, so cast to string or use chr() explicitly.
ctype_print() is true when every character produces some output, the space included. Over ASCII that is codepoints 32 to 126. Control characters (0 to 31) and DEL (127) make it false, which makes it the direct complement of ctype_cntrl().
When to reach for it
Use it as a safety gate before writing a value somewhere a control character would do damage: a log file, a terminal, a CSV cell, an HTTP header, a database column you expect to read back by eye. A single \x1b in a log line can rewrite a developer's terminal; a \x00 can truncate a value in code that touches C string APIs.
What trips people up
Newline is not printable by this definition, so any multi-line string returns false. That surprises people who expect "printable" to mean "human-readable text". If newlines are legitimate, split on them and test each line, or whitelist \n explicitly.
It is also a byte-level check, so UTF-8 text above ASCII returns false under the default C locale even though it displays fine.
Example
When to use it
- Rejecting log messages that contain ANSI escape sequences before writing them out
- Replacing non-printable bytes with a placeholder when dumping binary-tainted data
- Validating a single-line form field that must be safe to render in a terminal
Frequently asked questions
Does ctype_print consider a space printable?
Yes. The space is the one character that passes ctype_print but fails ctype_graph; every other difference between the two functions is nil.
Why does ctype_print return false on my multi-line string?
Line feed and carriage return are control characters, not printable ones. Split the string on newlines and test each line if line breaks are expected.
How do I strip non-printable characters from a string?
preg_replace('/[^\x20-\x7E]/', '', $text) removes everything outside printable ASCII in one call. Loop with ctype_print instead when you want to replace rather than delete, or keep certain control characters.
More examples
Masking non-printable bytes
Prints "log line. end" with the bell character replaced by a dot, then false for DEL (0x7F), which is a control character even though it sits above the printable range.
<?php
$raw = "log line\x07 end";
$safe = '';
foreach (str_split($raw) as $ch) {
$safe .= ctype_print($ch) ? $ch : '.';
}
echo $safe, "\n";
var_dump(ctype_print("\x7f"));Allowing newlines by testing line by line
Prints "line 0: ok" and "line 1: contains control bytes". Splitting on the newline removes the one control character you meant to allow, so the remaining failures are real problems.
<?php
$text = "first line\nsecond\x00line";
foreach (explode("\n", $text) as $i => $line) {
printf("line %d: %s\n",
$i,
ctype_print($line) ? 'ok' : 'contains control bytes');
}
Discussion