PHP - ctype_graph() 函式



PHP 字元型別檢查ctype_graph()函式用於確定給定字串是否僅由所有(可見的)可列印字元組成。“可列印”字元是指在輸出中可見的那些字元。

如果給定字串僅由(可見的)可列印字元組成,則此函式返回布林值true;否則,返回false。如果給定字串為空(""),此函式將始終返回“false”。

語法

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

ctype_graph(mixed $text): bool

引數

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

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

返回值

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

示例 1

以下程式演示了 PHP ctype_graph()函式的用法:

<?php
   $string = "HelloWorld";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

輸出

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

The given string is: HelloWorld
Does the string is printable? bool(true)

示例 2

如果給定文字中的每個字元都不是可列印的,則 PHP ctype_graph()函式返回false

<?php
   $string = "Tutorialpoint\t";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

輸出

上述程式產生以下輸出:

The given string is: Tutorialpoint
Does the string is printable? bool(false)

示例 3

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

<?php
   $string = "";
   echo "The given string is: $string";
   echo "\nDoes the string is printable? ";
   var_dump(ctype_graph($string));
?>

輸出

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

The given string is:
Does the string is printable? bool(false)

示例 4

檢查多個字串 (text).

在下面的示例中,我們使用 PHP ctype_graph()函式來檢查提供的字串是否全部由(可見的)可列印字元組成,並使用“for-each”迴圈遍歷給定的字串陣列:

<?php
   $string = array("Tutorialspoint", "India\t//r", "Hello@3&");
   echo "The given strings are: ";
   foreach($string as $text){
	   echo $text." ";
   }
   foreach($string as $test){
	   if(ctype_graph($test)){
		   echo "\nThe string '$test' consists of all (visibly) printable characters";
	   }
	   else{
		   echo "\nThe string '$test' does not consist of all (visibly) printable characters";
	   }
   }
   
?>

輸出

以下是上述程式的輸出:

The given strings are: Tutorialspoint India     //r Hello@3&
The string 'Tutorialspoint' consists of all (visibly) printable characters
The string 'India      //r' does not consist of all (visibly) printable characters
The string 'Hello@3&' consists of all (visibly) printable characters
php_function_reference.htm
廣告