PHP ctype_alnum() 函数用法及示例 - PHP教程

由网友 大卫 发布 阅读 2

PHP ctype_alnum() 函数用法及示例 - PHP教程

PHP Ctype 函数手册

ctype_alnum() 函数检测字符串是否全部为字母和(或)数字字符。

语法

ctype_alnum ( $text );

定义和用法

它检查提供的字符串,文本中的所有字符是否都是字母和(或)数字。

在标准C语言环境中,字母仅为[A-Za-z],该函数等效于preg_match('/^[a-z0-9]+$/iD',$ text)。

参数

序号参数和说明
1

text(必需)

要检测的字符串。

返回值

如果文本中的每个字符都是字母或数字,则返回TRUE,否则返回FALSE。

在线示例

遍历检测数组元素,是否全为字母数字

<?php
   $strings = array('hello', 'foo!#$bar');
   
   foreach ($strings as $example) {
      if (ctype_alnum($example)) {
         echo "$example 由字母或数字组成。\n";
      }else {
         echo "$example 不全是字母或数字。\n";
      }
   }
?>
测试看看‹/›

输出结果:

hello 由字母或数字组成。
foo!#$bar 不全是字母或数字。

PHP Ctype 函数手册

PHP closedir() PHP chroot()