ctype_print

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

ctype_print×ö¿É´òÓ¡×Ö·û¼ì²â

˵Ã÷

ctype_print ( string $text ) : bool

¼ì²éÌṩµÄ string ºÍ text ÀïÃæµÄ×Ö·ûÊDz»ÊǶ¼ÊÇ¿ÉÒÔ´òÓ¡³öÀ´¡£

²ÎÊý

text

ÐèÒª±»²âÊÔµÄ×Ö·û´®¡£

·µ»ØÖµ

Èç¹ûÔÚµ±Ç°µÄÓïÑÔ»·¾³Ï text ÀïÃæµÄÿ¸ö×Ö·û¶¼Äܱ»Êµ¼ÊÊä³ö£¨°üÀ¨¿Õ°×£©£¬¾Í·µ»Ø true £»Èç¹û text ÀïÃæ°üº¬¿ØÖÆ×Ö·û»òÕßÄÇЩ¸ù±¾²»»áÓÐÈκÎÊä³öµÄ×Ö·û´®£¬¾Í·µ»Ø false ¡£

·¶Àý

Example #1 Ò»¸ö 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";
    }
}
?>

ÒÔÉÏÀý³Ì»áÊä³ö£º

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.

×¢ÊÍ

Note:

Èç¹û¸ø³öÒ»¸ö -128 µ½ 255 Ö®¼ä(º¬)µÄint, ½«»á±»½âÊÍΪ¸ÃÖµ¶ÔÓ¦µÄASCII×Ö·û (¸ºÖµ½«¼ÓÉÏ 256 ÒÔÖ§³ÖÀ©Õ¹ASCII×Ö·û). ÆäËüÕûÊý½«»á±»½âÊÍΪ¸ÃÖµ¶ÔÓ¦µÄÊ®½øÖÆ×Ö·û´®.

²Î¼û

  • ctype_cntrl() - ×ö¿ØÖÆ×Ö·û¼ì²â
  • ctype_graph() - ×ö¿É´òÓ¡×Ö·û´®¼ì²â£¬¿Õ¸ñ³ýÍâ
  • ctype_punct() - ¼ì²â¿É´òÓ¡µÄ×Ö·ûÊDz»ÊDz»°üº¬¿Õ°×¡¢Êý×ÖºÍ×Öĸ

User Contributed Notes

harry at upmind dot com 22-Oct-2021 02:22
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);
    }
?>
ClayDragon 02-Sep-2018 09:20
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
Anonymous 12-Jun-2012 05:59
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.
PHP8ÖÐÎÄÊÖ²á ÓÉ Õ¾³¤ÔÚÏß ÕûÀí °æȨ¹éPHPÎĵµ×éËùÓÐ