php Ctype函數使用說明詳解

來源:互聯網
上載者:User

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)
檢查是否是只包含類是“nrt”之類的字 符控制字元


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
$str1 = 'Azxc1234';//true
$str2 = '123#Axy';//false
if (ctype_alnum($string)) {
    echo "This string totally works";
}
else {
    echo "And this one not so much";
}

注意:如果傳第一個Null 字元串,在php5.1之後是返回 false,但是早期的版本是返回true.

只要確保傳遞給函數的參數是一個字串就不會有問題。

 代碼如下 複製代碼

<?php
$integer = 42;
ctype_digit($integer);         // false
ctype_digit((string)$integer); // true

結論

在使用PHP的過程中,同樣的操作可能有不同的方法,選擇一個合適的方法顯然很重要。在PHP裡判斷字串類型的函數有兩類,ctype_*和 is_* 他們在實際的應用中 is_*這一系列的函數用的更多一些。當然除了這些辦法我們還可以選擇使用Regex來做判斷,而且更強大,但是使用PHP內建的函數可以更好地減少出錯率。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.