Core\Validate::isPrintable
Core\Validate::isPrintable(string $s): boolCore\Validate::isPrintable( string $s, ): bool
Checks whether a text contains no control characters. A control character has no visible form. It tells a terminal or a file what to do, for example start a new line. The method returns true or false.
A tab, a new line, a carriage return, the null byte and DEL are control characters, so the result is false. Letters from any language are printable, such as é, ß or 日. Emoji are printable too. The empty text contains no control character, so the result is true.
Use it for a short text from a user that must stay on one line, such as a name or a title. A new line in a name can make one entry in a log look like two.
Some invisible characters are not control characters, such as the zero-width joiner. They give true, so this method does not find text that hides what it really says.
The examples below check a few texts, show how isPrintable differs from Core\Validate::isAscii, and check a display name before it is saved.
Parameters
$sstringThe text to test.
Return value
Returns bool. true when the text has no control character, and true for the empty string. false for a tab, a new line, a carriage return, DEL or any other control character. Some invisible characters are not control characters, such as the zero-width joiner. They give true, so this method does not find text that hides what it really says.
Examples
Check a few texts
<?nvs// Shows which texts contain only printable characters.
// The third text has a new line, and the fourth has a tab.// "é" and "日本" are letters, so they are printable.array<string> $texts = ['Order 1042', 'Café 日本', "line one\nline two", "name\tvalue"];
// `Core\Validate::isPrintable` returns `false` when a text contains a control character.// This prints yes for the first two texts and no for the other two.int $number = 0;foreach ($texts as string $text) { $number = $number + 1; echo 'text ', $number, ': ', Core\Validate::isPrintable($text) ? 'yes' : 'no', "\n";}text 1: yestext 2: yestext 3: notext 4: noPrintable is not ascii
<?nvs// Shows that `isPrintable` and `isAscii` check two different things.
// "café" has a letter that is not ASCII. "a\tb" is all ASCII, but a tab is a control character.array<string> $texts = ['cafe', 'café', "a\tb"];
// This prints "ascii yes, printable yes", then "ascii no, printable yes",// then "ascii yes, printable no".foreach ($texts as string $text) { echo 'ascii ', Core\Validate::isAscii($text) ? 'yes' : 'no'; echo ', printable ', Core\Validate::isPrintable($text) ? 'yes' : 'no', "\n";}ascii yes, printable yesascii no, printable yesascii yes, printable noCheck a display name
<?nvs// Shows how to check a display name from a sign-up form before a program saves it.
// The second name has a new line in it. Written to a log, it would look like two lines.array<string> $names = ['Zoë Müller', "admin\nlogin ok", '李雷'];
// A name with a control character is not saved.// This saves the first and the third name.foreach ($names as string $name) { if (Core\Validate::isPrintable($name)) { echo 'saved: ', $name, "\n"; } else { echo "not saved: the name contains a control character\n"; }}saved: Zoë Müllernot saved: the name contains a control charactersaved: 李雷