PHP - ctype_alpha() 函式



PHP 字元型別檢查ctype_alpha() 函式用於檢查提供的字串中的所有字元是否都是字母。 “字母”字元是指從 A 到 Z 的字母,包括大寫和小寫。例如,在字串“hello”中,字元 'h'、'e'、'l'、'l' 和 'o' 都是字母字元。

如果提供的字串包含任何非字母字元(例如數字或標點符號),則此函式返回布林值false;否則,它返回true。如果提供的文字為空(""),則此函式始終返回 'false'。

語法

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

ctype_alpha (mixed $text): bool

引數

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

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

返回值

如果 text 中的每個字元都是字母,則此函式返回 'true';否則,它返回 'false'。

示例 1

如果給定的文字(字串)“僅包含”字母,則 PHP ctype_alpha() 函式將返回true

<?php
   $text = "Tutorialspoint";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

輸出

以下是上述程式的輸出:

The given text: Tutorialspoint
Is the text is alphanumeric? bool(true)

示例 2

如果給定的文字(字串)“不只包含”字母,則 PHP ctype_alpha() 函式將返回false

<?php
   $text = "Tutorix12";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

輸出

上述程式產生以下輸出:

The given text: Tutorix12
Is the text is alphanumeric? bool(false)

示例 3

如果提供的文字或字串為空"",則此函式將始終返回false

<?php
   $text = " ";
   echo "The given text: ".$text;
   #using ctype_alpha() function
   echo "\nIs the text is alphanumeric? ";
   var_dump(ctype_alpha($text));
?>

輸出

執行上述程式後,它將返回 'false':

The given text:
Is the text is alphanumeric? bool(false)
php_function_reference.htm
廣告