PHP - ctype_space() 函式



PHP 字元型別檢查的 ctype_space() 函式用於判斷給定字串中的所有字元是否都是空白字元。空白字元包括空格、製表符 (\t)、換行符 (\n)、回車符 (\r) 等類似字元。

如果給定字串包含空白字元,此函式返回布林值 true;否則返回 false。如果給定字串為空 (""),此函式始終返回 "false"。

語法

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

ctype_space (mixed $text): bool

引數

此函式接受以下引數:

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

返回值

如果 text 中的每個字元都是某種型別的空白字元,則此函式返回 "true";否則返回 "false"。

示例 1

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

<?php
   $string = "\n\r\t";
   echo "The given string is: $string";
   echo "Does the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

輸出

以上程式輸出如下:

The given string is:
        Does the string '
        ' contain characters that create white space? bool(true)

示例 2

如果提供的字串不包含空白字元,PHP ctype_space() 函式將返回 false

<?php
   $string = "Tutorialspoint@#5";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

輸出

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

The given string is: Tutorialspoint@#5
Does the string 'Tutorialspoint@#5' contain characters that create white space? bool(false)

示例 3

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

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' contain characters that create white space? ";
   #using ctype_space() function
   var_dump(ctype_space($string));
?>

輸出

執行以上程式後,將生成以下輸出:

The given string is:
Does the string '' contain characters that create white space? bool(false)

如果輸入字串為空 (" ") 但包含空格,此函式將返回 'true',因為它檢查輸出中是否存在空白字元。

php_function_reference.htm
廣告