PHP - ctype_punct() 函式



PHP 字元型別檢查ctype_punct()函式用於檢查給定字串是否僅包含標點符號。 “標點符號”表示它不應包含任何字母、數字或空格。 字串只能包含標點符號,例如“&”、“$”、“()”和“^”。

如果給定字串(文字)中的每個字元都是可列印的,但它不應是字母、數字或空格,則此函式返回true;否則,它返回false。如果給定字串為空(""),則此函式始終返回“false”。

語法

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

ctype_punct(mixed $text): bool

引數

以下是此函式的引數:

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

返回值

如果文字中的每個字元都可列印,則此函式返回“true”,否則返回“false”。

示例 1

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

<?php
   $string = "*&$()^";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all punctuation? ";
   var_dump(ctype_punct($string));
?>

輸出

以下是上述程式的輸出:

The given string is: *&$()^
Does the string '*&$()^' consist of all punctuation? bool(true)

示例 2

如果給定字串包含任何可列印的字元(空格或字母數字(字母和數字)),則 PHP ctype_punct()函式將返回false

<?php
   $string = "Tutorialspoint!#@!!&";
   echo "The given string is: $string";
   echo "\nDoes the string '$string' consist of all punctuation? ";
   var_dump(ctype_punct($string));
?>

輸出

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

The given string is: Tutorialspoint!#@!!&
Does the string 'Tutorialspoint!#@!!&' consist of all punctuation? bool(false)

示例 3

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

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

輸出

執行上述程式後,它會生成以下輸出:

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