PHP 8.3.4 Released!

ctype_print

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

ctype_printVerifica se os caracteres são imprimíveis

Descrição

ctype_print(mixed $text): bool

Verifica se todos os caracteres na string fornecida, text, são imprimíveis.

Parâmetros

text

A string a ser testada.

Nota:

Se um int entre -128 e 255 inclusive for fornecido, ele será interpretado como o valor ASCII de um único caractere (valores negativos são somados a 256 para permitir caracteres no intervalo ASCII estendido). Qualquer outro inteiro será interpretado como uma string contendo os dígitos decimais do inteiro.

Aviso

A partir do PHP 8.1.0, passar um argumento que não seja string tornou-se defasado. No futuro, o argumento será interpretado como uma string em vez de um ponto de código ASCII. Dependendo do comportamento esperado, o argumento deve ser convertido para string ou uma chamada explícita para chr() deve ser feita.

Valor Retornado

Retorna true se todos caracteres em text irá produzir saída (incluindo em branco). Retorna false se text contem caracteres de controle ou caracteres que não tem uma saída ou funções de controle inteiramente. Quando chamada com uma string vazia, o resultado sempre será false.

Exemplos

Exemplo #1 Um exemplo da ctype_print()

<?php
$strings
= array('string1' => "asdf\n\r\t", 'string2' => 'arf12', 'string3' => 'LKA#@%.54');
foreach (
$strings as $name => $testcase) {
if (
ctype_print($testcase)) {
echo
"The string '$name' consists of all printable characters.\n";
} else {
echo
"The string '$name' does not consist of all printable characters.\n";
}
}
?>

O exemplo acima produzirá:

The string 'string1' does not consist of all printable characters.
The string 'string2' consists of all printable characters.
The string 'string3' consists of all printable characters.

Veja Também

  • ctype_cntrl() - Verifica se os caracteres são de controle
  • ctype_graph() - Verifica se os caracteres são imprimíveis exceto espaço
  • ctype_punct() - Verifica se é um caractere imprimível que não é whitespace ou alfanumério
  • IntlChar::isprint() - Verifica se um ponto de código é um caractere imprimível

add a note

User Contributed Notes 4 notes

up
4
ClayDragon
5 years ago
As mentioned above, only ASCII characters from 32 to 126 are considered printable, all others, including UTF-8 encoded characters are always considered unprintable, no matter what your locale settings are. Therefore, e.g. German "ä", the Euro sign "€" or the British pound symbol "£" will never be printable. If you need to check any characters for "printability" beyond the standard ASCII range, use a regular expression or write a specific function yourself.

See also this discussion: https://grokbase.com/t/php/php-i18n/102tkqe6rk/ctype-print-returns-false-for-british-pound-symbol-and-non-ascii-symbols
up
3
Anonymous
11 years ago
Only ascii 32 thru 126 (inclusive) are considered printable. Tab (ascii 7), carriage return (ascii 13), linefeed (ascii 10) etc may produce output but are not considered printable.
up
2
harry at upmind dot com
2 years ago
You can use this function to detect if a string is "binary".

<?php
/**
* Determine whether the given value is a binary string by checking to see if it contains only printable characters.
*
* @param string $value
*
* @return bool
*/
function isBinary($value): bool
{
// remove "unprintable" whitespace characters (tabs, newlines etc)
$string = preg_replace('/\s/', '', (string)$value);

return !empty(
$string) && !ctype_print($string);
}
?>
up
0
Kalahiri
1 year ago
To check whether a string consists only of ASCII characters, use mb_detect_encoding( $string, 'ASCII', true );
To Top