移除字符串末尾的最后一個字符
1.substr()方法
$string = 'hello kitty';
substr($string,0,-1);
移除字符串右側字符
1.chop()方法
$string = 'Hello world!';
echo chop($string,'world!');
結果: Hello
值得注意的是(1)chop方法不會改變原字符串;(2)參數末尾必須與字符串末尾字符一致,否則無效。(3)據參考資料講,當參數未填時,會處理一些特殊字符,例如換行符,空格等,本人未測試出來。歡迎交流。
2.rtrim()方法
移除字符串左側字符
1.ltrim() 方法
移除字符串兩側字符
1.trim()方法
平均分割字符串
1.chunk_split()方法
$string = 'Hello world!';
echo chunk_split($string,2,'?');
結果:He?ll?o ?wo?rl?d!?
值得注意的是:(1)一個空格占據一個字符的位置;
使用一個字符串去分割另一個字符串,並返回由字符串組成的數組
1.explode()方法
$str = 'Hello world! How are you?';
print_r(explode(' ',$str,-1));
結果:Array ( [0] => Hello [1] => world! [2] => How [3] => are )
值得注意的是:可選的第三個參數 n 分幾種情況:(1)n>0 返回包含最多 limit 個元素的數組;(2) n=0 相當於n=1;(3)n<0 將返回的數組去除最后一個元素。
2.serialize();unserialize();
計算字符串中字符個數:
1.strlen('中文字符') 結果:12 因為一個中文占 3 個字符數
2.mb_strlen('中文字符','utf-8'); 結果:4