PHP - ctype_cntrl() 函式



PHP 字元型別檢查ctype_cntrl()函式用於檢查給定字串中的所有字元是否都是控制字元。控制字元是不可列印的字元,用於控制文字的處理或顯示方式,例如換行符(\n)、製表符(\t)等。

如果給定字串僅由控制字元組成,則此函式返回布林值true;否則,返回false。如果給定字串為空(""),此函式將始終返回 "false"。

語法

以下是 PHP 字元型別檢查ctype_cntrl()函式的語法:

ctype_cntrl(mixed $text): bool

引數

此函式接受一個引數,如下所述:

  • text (必需) - 需要測試的字串。

返回值

如果 text 中的每個字元都是控制字元,則此函式返回 "true",否則返回 "false"。

示例 1

以下是 PHP ctype_cntrl()函式的基本示例:

<?php
   $string = "\t\n";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consists of all control characters? ";
   var_dump(ctype_cntrl($string));
?>

輸出

上述程式輸出如下:

The given string is:

Does the string '
' consists of all control characters? bool(true)

示例 2

如果給定字串並非全部由控制字元組成,則 PHP ctype_cntrl()函式將返回 false

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

輸出

執行上述程式後,將顯示以下輸出:

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

示例 3

如果給定字串為空(""),此函式將始終返回 false

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

輸出

以下是上述程式的輸出:

The given string is:
Does the string ' ' consist of all control characters? bool(false)
php_function_reference.htm
廣告