字串常用函式
獲取字串長度函式
/*
* strlen函式
* int strlen($var)
* 獲取字串或數字的長度
*/
$a = 'hello, woRld';
$b = '王昭'; //utf8格式下,每個漢字3個位元組長
$c = 1111;
echo strlen($a),"\n", strlen($b), strlen($c), "\n";
大小寫轉換函式
/*
* string strtolower(string $str):字串所有的字母轉換為小寫
* string strtoupper(string $str):字串所有的字母轉換為大寫
*
* string ucfirst(string $str):將字串的首字母大寫,其他字母不變
* string ucwords(string $str):將字串中每個單詞的首字母大寫,其他字母不變
*
*/
$a = 'hello, world zhanGsan';
echo strtolower($a), "\n";
echo strtoupper($a), "\n";
echo ucfirst($a), "\n";
echo ucwords($a), "\n";
字串替換函式
//字串替換函式
/*
* str_replace($search, $replace, $str):實作字串替換,區分大小寫
* str_ireplace($search, $replace, $str):實作字串的替換,不區分大小寫
*
* $search:被替換字串
* $replace:替換字串
* $str:主字串
*
*/
$a = 'this is a test';
echo str_replace('is','is\'t', $a), "\n";
echo str_ireplace('THIS', 'that', $a), "\n";
//將'ZenD_CONTRollER_FronT'變成'Zend_Controller_Front'
$str = 'ZenD_CONTRollER_FronT';
$str = strtolower($str);
$str = str_replace('_', ' ', $str);
$str = ucwords($str);
$str = str_replace(' ', '_', $str);
echo $str, "\n";
和html物體相關的函式
/*
* htmlspecialchars函式
* string htmlspecialchars(string $str)
* 描述:預定義的字符轉換為html物體
*
*/
$a = 'A>B, B<A';
echo htmlspecialchars($a), "\n";
洗掉空白或其他字符相關的函式
/*
* ltrim函式
* string ltrim(string $str[, string $charlist])
* 描述:實作洗掉字串開始位置的空格或其他字符
* charlist規定從字串中洗掉哪些字符,如果省略該引數,則移除所有的空白字符(空格、換行、回車等)
*
* rtrim函式
* string rtrim(string $str[, string $charlist])
* 描述:實作洗掉字串結束位置的空格或其他字符
*
* trim函式
* string trim(string $str[, string $charlist])
* 描述:實作洗掉字串開始和結束的位置的空格或者其他字符
*
*/
$a = ' ABC ';
echo $a, '長度為'.strlen($a), "\n";
echo ltrim($a), '長度為'.strlen(ltrim($a)), "\n";
echo rtrim($a), '長度為'.strlen(rtrim($a)), "\n";
echo trim($a), '長度為'.strlen(trim($a)), "\n";
字串位置相關的函式
/*
* strpos函式
* int strpos(string haystack, mixed needle [,int offset])
* 描述:將回傳一個字串在另一個字串第一次出現的位置,區分大小寫
*
* stripos函式
* int strpos(string haystack, mixed needle [,int offset])
* 描述:將回傳一個字符在另一個字符第一次出現的位置,忽略大小寫
*
* strrpos函式
* int strrpos(string haystack, mixed needle [,int offset])
* 描述:將回傳一個字串在另一個字串最后一次出現的位置,區分大小寫
*
* strripos函式
* int strripos(string haystack, mixed needle [,int offset])
* 描述:將回傳一個字串在另一個字串最后一次出現的位置,忽略大小寫
*
*/
$a = 'this is test';
echo strpos($a, 'is'), "\n";
//echo strpos($a,'Is'), "\n";
var_dump(strpos($a,'Is')); //不存在,回傳false
echo stripos($a,'Is'), "\n"; //忽略大小寫,存在
echo strrpos($a, 'is'), "\n";
echo strripos($a,'Is'), "\n";
字串截取函式
/*
* substr函式
* string substr(string $str, int $start[, int $length])
* 描述:截取字串
* 說明:如果省略length,則回傳從start至字串結尾之間的字串
* 如果startw為負數,則倒數,如果length為負數,表示從開始位置截取到結束位置
*
*/
$str = 'javascript';
echo substr($str, 5), "\n";
echo substr($str, 0, 5), "\n";
echo substr($str, -5, 5), "\n";
echo substr($str, -5,-2), "\n";
//得到檔案的擴展名
$str = 'a.b.c.txt';
$locate = strrpos($str, '.'); //獲取最后一個點的位置
echo substr($str, $locate+strlen('.')), "\n"; //截取點后面的字串,即是拓展名
字串截取函式
/*
* strstr函式
* string strstr(string $haystack, mixed $needle)
* 描述:將搜索一個字串在另一個字串中第一次出現的位置,然后回傳字串的其余部分,區分大小寫
*
* stristr函式
* string stristr(string $haystack, mixed $needle)
* 描述:將搜索一個字串在另一個字串中第一次出現的位置,然后回傳字串的其余部分,忽略大小寫
*
* strrchr函式
* string strrchr(string $haystack, mixed $needle)
* 描述:將搜索字串在另一個字串中最后一次出現的位置,然后回傳字串的其余部分,區分大小寫
*
*/
$str = 'this Is a test';
echo strstr($str, 'is'), "\n";
echo stristr($str, 'is'), "\n";
echo strrchr($str, 'is'),"\n";
//得到檔案的擴展名
$str = 'a.b.c.txt';
echo substr(strrchr($str, '.'), 1),"\n";
反轉字串函式
/*
* strrev函式
* string strrev(string $string)
* 描述:反轉字串
*
*/
$str = 'hello, world';
echo strrev($str),"\n";
字串加密函式
/*
* md5函式
* string md5(string $str)
* 描述:實作計算字串的md5哈希值
*
* str_shuffle函式
* string str_shuffle(string $str)
* 描述:隨機打亂字串,可用于產生隨機驗證碼
*/
$str = 'imooc';
echo md5($str),"\n";
echo str_shuffle($str),"\n";
分割字串函式
/*
* explode函式
* array explode(string $delimiter, string $string[, int $limit])
* 描述:使用一個字串分割另一個字串,回傳一個陣列,$limit限制陣列內元素的個數
*
* implode函式
* string implode(string $glue, array $pieces)
* string implode(array $pieces)
* 描述:將一個一維陣列的值轉化為字串
*
*/
$str = 'this-is-a-test';
$arr = explode('-', $str);
print_r($arr);
echo implode('-', $arr),"\n"; //使用'-'將陣列內元素連接起來
echo implode($arr),"\n"; //將陣列內元素連接起來,功能和'.'相同
格式化字串函式
/*
* sprintf函式
* string sprintf(string $format[, mixed $args[, mixed $...]])
* 描述:格式化字串,和OC中NSLog(..)類似
* 注意:如果%符號多于arg引數,則必須使用占位符,占位符位于%符號之后,由數字和"\$"組成
*
* $format引數,規定字串以及宣告變數的格式型別,取值為:
* %%:回傳一個百分號%
* %b:二進制數
* %d:包含正負號的十進制數(負數、0、正數)
* %e:使用小寫的科學計數法(例如:1.2e+2)
* %s:字串
* %f:浮點數
*
* 附加的格式,必須放置在%和字母之間(例如%.2f):
* - + :定義數字的正負
* [0-9]:規定變數值的最小寬度
* .[0-9]:規定小數位數或最大字串長度
*
*/
$num = 5;
$str = 'Tom';
echo sprintf("this is %d test, %s", $num, $str),"\n";
echo sprintf("this is %1\$s test, %1\$s", $str),"\n";
echo sprintf("帶兩位小數:%1\$.2f 不帶小數:%1\$d", $num),"\n";
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69418.html
標籤:PHP
上一篇:php流程控制
下一篇:php函式
