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
广告