php ctype函數中文翻譯和樣本_php執行個體

來源:互聯網
上載者:User

PHP Ctype擴充是PHP4.2開始就內建的擴充,注意,Ctype系列函數都只有一個字串型別參數,它們返回布爾值。

複製代碼 代碼如下:

$str = "0.1123";
//檢查字串所有字元是否為數字
echo "ctype_digit:" . ctype_digit($str);  //空
//檢測是否為數字字串,可為負數和小數
echo "is_numberic:" . is_numeric($str); //1

從上面可以看出ctype_digit()和is_numberic()的區別。

中文翻譯

Ctype函數是PHP內建的字串體測函數。主要有以下幾種

ctype_alnum -- Check for alphanumeric character(s)
檢測是否是只包含[A-Za-z0-9]

ctype_alpha -- Check for alphabetic character(s)
檢測是否是只包含[A-Za-z]

ctype_cntrl -- Check for control character(s)
檢查是否是只包含類是“\n\r\t”之類的字 符控制字元

ctype_digit -- Check for numeric character(s)
檢查時候是只包含數字字元的字串(0-9)

ctype_graph -- Check for any printable character(s) except space
檢查是否是只包含有可以列印出來的字元(除了空格)的字串

ctype_lower -- Check for lowercase character(s)
檢查是否所有的字元都是英文字母,並且都是小寫

ctype_print -- Check for printable character(s)
檢查是否是只包含有可以列印出來的字元的字串

ctype_punct -- Check for any printable character which is not whitespace or an alphanumeric character
檢查是否是只包含非數字/字元/空格的可列印出來的字元

ctype_space -- Check for whitespace character(s)
檢查是否是只包含類是“ ”之類的字元和空格

ctype_upper -- Check for uppercase character(s)
檢查是否所有的字元都是英文字母,並且都是大寫的

ctype_xdigit -- Check for character(s) representing a hexadecimal digit
檢查是否是16進位的字串,只能包括 “0123456789abcdef”

有樣本的喲

我們平常在遇到要對一些表單做簡單過濾的時候,往往不太願意寫正則,而且在效率上,正則也是影響PHP運行速度的原因之一,所以在能不試用正則的時候盡量不試用正則。幸好PHP已經為我們考慮到了這一點,給我提供了Ctype函數。下面對一些Ctype函數做一些簡單介紹,以備用:
1、ctype_alnum — Check for alphanumeric character(s)   檢查字串中只包含數字或字母,相當於正則[A-Za-z0-9].   有傳回值。成功時返回TRUE,失敗為FALSE;
[

複製代碼 代碼如下:

<?php 
$strings = array('AbCd1zyZ9', 'foo!#$bar'); 
foreach ($strings as $testcase) { 
    if (ctype_alnum($testcase)) { 
        echo "The string $testcase consists of all letters or digits.\n"; \\ 輸出The string AbCd1zyZ9 consists of all letters or digits. 
    } else { 
        echo "The string $testcase does not consist of all letters or digits.\n"; \\ 輸出 The string foo!#$bar does not consist of all letters or digits. 
    } 

?> 

2、ctype_alpha — Check for alphabetic character(s)  檢查字串中只包含字母。  成功時返回TRUE,失敗為FALSE;

複製代碼 代碼如下:

<?php 
$strings = array('KjgWZC', 'arf12'); 
foreach ($strings as $testcase) { 
    if (ctype_alpha($testcase)) { 
        echo "The string $testcase consists of all letters.\n"; \\ 輸出 The string KjgWZC consists of all letters. 
    } else { 
        echo "The string $testcase does not consist of all letters.\n";<span style="white-space:pre">   </span>\\ 輸出 The string arf12 does not consist of all letters. 
    } 

?> 

3、ctype_cntrl — Check for control character(s)    檢查字串中是否只包含" '\n' '\r' '\t' " 這樣的控制字元。

複製代碼 代碼如下:

<?php 
$strings = array('string1' => "\n\r\t", 'string2' => 'arf12'); 
foreach ($strings as $name => $testcase) { 
    if (ctype_cntrl($testcase)) { 
        echo "The string '$name' consists of all control characters.\n"; \\ 輸出 The string 'string1' consists of all control characters. 
    } else { 
        echo "The string '$name' does not consist of all control characters.\n"; \\ The string 'string2' does not consist of all control characters. 
    } 

?>  

4、ctype_digit — Check for numeric character(s) 檢查字串中是否只包含數字

複製代碼 代碼如下:

<?php 
$strings = array('1820.20', '10002', 'wsl!12'); 
foreach ($strings as $testcase) { 
    if (ctype_digit($testcase)) { 
        echo "The string $testcase consists of all digits.\n"; 
    } else { 
        echo "The string $testcase does not consist of all digits.\n"; 
    } 

?>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.