php ctype函數(shù)中文翻譯和示例
PHP Ctype擴(kuò)展是PHP4.2開始就內(nèi)建的擴(kuò)展,注意,Ctype系列函數(shù)都只有一個(gè)字符串類型參數(shù),它們返回布爾值。
$str = "0.1123";
//檢查字符串所有字符是否為數(shù)字
echo "ctype_digit:" . ctype_digit($str); //空
//檢測(cè)是否為數(shù)字字符串,可為負(fù)數(shù)和小數(shù)
echo "is_numberic:" . is_numeric($str); //1
從上面可以看出ctype_digit()和is_numberic()的區(qū)別。
中文翻譯
Ctype函數(shù)是PHP內(nèi)置的字符串體測(cè)函數(shù)。主要有以下幾種
ctype_alnum -- Check for alphanumeric character(s)
檢測(cè)是否是只包含[A-Za-z0-9]
ctype_alpha -- Check for alphabetic character(s)
檢測(cè)是否是只包含[A-Za-z]
ctype_cntrl -- Check for control character(s)
檢查是否是只包含類是“\n\r\t”之類的字 符控制字符
ctype_digit -- Check for numeric character(s)
檢查時(shí)候是只包含數(shù)字字符的字符串(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
檢查是否是只包含非數(shù)字/字符/空格的可打印出來的字符
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進(jìn)制的字符串,只能包括 “0123456789abcdef”
有示例的喲
我們平常在遇到要對(duì)一些表單做簡(jiǎn)單過濾的時(shí)候,往往不太愿意寫正則,而且在效率上,正則也是影響PHP運(yùn)行速度的原因之一,所以在能不試用正則的時(shí)候盡量不試用正則。幸好PHP已經(jīng)為我們考慮到了這一點(diǎn),給我提供了Ctype函數(shù)。下面對(duì)一些Ctype函數(shù)做一些簡(jiǎn)單介紹,以備用:
1、ctype_alnum — Check for alphanumeric character(s) 檢查字符串中只包含數(shù)字或字母,相當(dāng)于正則[A-Za-z0-9]. 有返回值。成功時(shí)返回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) 檢查字符串中只包含字母。 成功時(shí)返回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) 檢查字符串中是否只包含數(shù)字
<?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";
}
}
?>
- PHP中數(shù)字檢測(cè)is_numeric與ctype_digit的區(qū)別介紹
- php中debug_backtrace、debug_print_backtrace和匿名函數(shù)用法實(shí)例
- php中fgetcsv()函數(shù)用法實(shí)例
- PHP中SimpleXML函數(shù)用法分析
- PHP中array_slice函數(shù)用法實(shí)例詳解
- php中in_array函數(shù)用法探究
- PHP中mysql_field_type()函數(shù)用法
- php中mt_rand()隨機(jī)數(shù)函數(shù)用法
- PHP中substr()與explode()函數(shù)用法分析
- PHP中shuffle數(shù)組值隨便排序函數(shù)用法
- php中Ctype函數(shù)用法詳解
相關(guān)文章
基于thinkPHP實(shí)現(xiàn)的微信自定義分享功能示例
這篇文章主要介紹了基于thinkPHP實(shí)現(xiàn)的微信自定義分享功能,結(jié)合實(shí)例形式分析了thinkPHP調(diào)用微信接口實(shí)現(xiàn)自定義分享功能的相關(guān)操作技巧,需要的朋友可以參考下2016-09-09laravel使用數(shù)據(jù)庫測(cè)試注意事項(xiàng)
這篇文章主要介紹了laravel使用數(shù)據(jù)庫測(cè)試注意事項(xiàng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法
這篇文章主要介紹了php靜態(tài)成員方法和靜態(tài)的成員屬性的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家使用的時(shí)候注意方法,需要的朋友可以參考下2017-10-10使用YUI+Ant 實(shí)現(xiàn)JS CSS壓縮
YUI庫是一組工具和控件,它們用JavaScript寫成, 為的是用DOM 腳本,DHTML和AJAX等技術(shù)創(chuàng)建豐富的網(wǎng)頁交互式應(yīng)用程序。YUI 基于BSD協(xié)議,對(duì)所有的使用方式都是免費(fèi)的。YUI 項(xiàng)目包括YUI 庫和兩個(gè)創(chuàng)建時(shí)工具:YUI Compressor (壓縮)和YUI Doc(JavaScripts代碼的文檔引擎)2014-09-09php定時(shí)執(zhí)行任務(wù)設(shè)置詳解
這篇文章主要介紹了php定時(shí)執(zhí)行任務(wù)設(shè)置的方法,非常簡(jiǎn)單,有需要的小伙伴參考下。2015-02-02深入理解 PHP7 中全新的 zval 容器和引用計(jì)數(shù)機(jī)制
這篇文章主要介紹了 PHP7 中全新的 zval 容器和引用計(jì)數(shù)機(jī)制的相關(guān)知識(shí), 主要側(cè)重于解釋新 zval 容器中的引用計(jì)數(shù)機(jī)制。需要的朋友可以參考下2018-10-10