<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/refs.basic.vartype.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'book.ctype.php',
    1 => 'Ctype',
    2 => '字符类型检测',
  ),
  'up' => 
  array (
    0 => 'refs.basic.vartype.php',
    1 => '变量与类型相关扩展',
  ),
  'prev' => 
  array (
    0 => 'function.trait-exists.php',
    1 => 'trait_exists',
  ),
  'next' => 
  array (
    0 => 'ctype.setup.php',
    1 => '安装/配置',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/ctype/book.xml',
  ),
  'history' => 
  array (
  ),
  'extra_header_links' => 
  array (
    'rel' => 'alternate',
    'href' => '/manual/en/feeds/book.ctype.atom',
    'type' => 'application/atom+xml',
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="book.ctype" class="book">
 
 <h1 class="title">字符类型检测</h1>
 

 <div id="intro.ctype" class="preface">
  <h1 class="title">简介</h1>
  <p class="para">
   该扩展所提供的函数用来检测
   在当前的区域设定下（参见 <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span>）检查字符或者字符串
   是否仅包含指定类型的字符。
  </p>
  <p class="para">
   当使用整数作为参数调用这些函数的时候，
   这些函数的行为和 C 语言（<var class="filename">ctype.h</var>）
   中的对应函数一样。
   也就是说，如果传入了小于 256 的整数参数，它会把这个整数当做 ASCII 码值，
   来检测对应字符是否符合要求（数值检测的 ASCII 值范围为 0x30-0x39）。
   如果传入的参数介于 -128 和 -1 之间（含 -128 和 -1），它会先将其加上 256，然后再进行检测。
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="para">
自 PHP 8.1.0 起，弃用传递非字符串参数。未来该参数将解释为字符串而不是 ASCII 
码点。根据预期行为，应将参数转为字符串或显式调用 <span class="function"><a href="function.chr.php" class="function">chr()</a></span>。</p></div>
  <p class="para">
   当传入参数为字符串的时候，它会对字符串中的每个字符进行检测，
   仅当字符串中的每个字符都符合指定条件的情况下才会返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
   当使用空字符串调用时，结果始终为 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <p class="para">
   传入除整数和字符串之外的参数
   会立即返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <p class="para">
   需要提醒的是，如果可以满足需求，请优先考虑使用 ctype 函数，
   而不是正则表达式或者对应的 <code class="literal">&quot;str_*&quot;</code> 和 <code class="literal">&quot;is_*&quot;</code> 函数。
   因为 ctype 使用的是原生 C 库，所以会有明显的性能优势。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    这些函数和 Python 中的 “ctypes” 库没有任何关系。
    ctype 的名字源自对应的 C 语言中的头文件 <var class="filename">ctype.h</var>。
   </p>
   <p class="para">
    实际上，这个扩展是先于 Python 的 “ctypes”，所以该命名引起的任何困扰都不是 PHP 的错。
   </p>
  </p></blockquote>
 </div>

 









 










<ul class="chunklist chunklist_book"><li><a href="ctype.setup.php">安装/配置</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="ctype.requirements.php">需求</a></li><li><a href="ctype.installation.php">安装</a></li></ul></li><li><a href="ref.ctype.php">Ctype 函数</a><ul class="chunklist chunklist_book chunklist_children"><li><a href="function.ctype-alnum.php">ctype_alnum</a> — 检测字母数字式字符</li><li><a href="function.ctype-alpha.php">ctype_alpha</a> — 检测字母字符</li><li><a href="function.ctype-cntrl.php">ctype_cntrl</a> — 检测控制字符</li><li><a href="function.ctype-digit.php">ctype_digit</a> — 检测数字字符</li><li><a href="function.ctype-graph.php">ctype_graph</a> — 检测除空格外的任何打印字符</li><li><a href="function.ctype-lower.php">ctype_lower</a> — 检测小写字符</li><li><a href="function.ctype-print.php">ctype_print</a> — 检测可打印字符</li><li><a href="function.ctype-punct.php">ctype_punct</a> — 检测可打印的字符是不是不包含空白、数字和字母</li><li><a href="function.ctype-space.php">ctype_space</a> — 检测空白字符</li><li><a href="function.ctype-upper.php">ctype_upper</a> — 检测大写字符</li><li><a href="function.ctype-xdigit.php">ctype_xdigit</a> — 检测字符是否只包含十六进制字符</li></ul></li></ul></div><?php manual_footer($setup); ?>