1.wordwrap 打斷字串為指定數量的字串
$text="shang ban jiu shi wei le huo zhe ";
$new_text=wordwrap($text,20,"<br/>\n");
print_r($new_text);
2.vsprintf 返回格式化字串
print_r(vsprintf("%04d-%02d-%02d",explode('-','2020-10-1')));
3.vprintf 輸出格式化字串
print_r(vprintf("%04d-%02d-%02d",explode('-','2020-10-1')));
4. ucwords將字串中每個單詞的首字母轉換為大寫
$text1='come on';
echo(ucwords($text1)).("<br/>\n");
$text2='COME ON';
echo(strtolower($text2)).("<br/>\n");
echo(ucwords(strtolower($text2))).("<br/>\n");
$text3='come|on';
echo(ucwords($text3)).("<br/>\n");
echo(ucwords($text3,'|')).("<br/>\n");
5.ucfirst 將字串的首字母轉換為大寫
$text1='come on';
echo(ucfirst($text1)).("<br/>\n");
$text2='COME ON';
echo(strtolower($text2)).("<br/>\n");
echo(ucfirst(strtolower($text2))).("<br/>\n");
$text3='come|on';
echo(ucfirst($text3)).("<br/>\n");
6.trim 去除字串收尾處的空白字元(或其他字元)
$text1=' come on ';
echo(trim($text1)).("<br/>\n");
$text2='CCOME ONC';
echo(trim($text2,'C')).("<br/>\n");
$text3='come on|';
echo(trim($text3,'|')).("<br/>\n");
7.substr 返回字串的子串
$rest = substr("abcdef", 1);
$rest = substr("abcdef", 1,4);
$rest = substr("abcdef", 0, -1);
$rest = substr("abcdef", 2, -1);
$rest = substr("abcdef", 4, -4);
$rest = substr("abcdef", -3, -1);
8.substr_replace替換字串的子串
echo substr_replace("Hello","world",0);
echo substr_replace("Hello world","Shanghai",6);
echo substr_replace("Hello world","Shanghai",-5);
echo substr_replace("world","Hello ",0,0);
$replace = array("1: AAA","2: AAA","3: AAA");
echo implode("<br>",substr_replace($replace,'BBB',3,3));
9.substr_count()計算字串出現的次數
$text = 'This is a test';
echo strlen($text);
echo substr_count($text, 'is');
echo substr_count($text, 'is', 3);
echo substr_count($text, 'is', 3, 3);
echo substr_count($text, 'is', 5, 10);
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
10.substr_compare 二進位制安全比較字串(從偏移位置比較指定長度)
echo substr_compare("abcde", "bc", 1, 2);
echo substr_compare("abcde", "de", -2, 2);
echo substr_compare("abcde", "bcg", 1, 2);
echo substr_compare("abcde", "BC", 1, 2, true);
echo substr_compare("abcde", "bc", 1, 3);
echo substr_compare("abcde", "cd", 1, 2);
echo substr_compare("abcde", "abc", 5, 1);
本作品採用《CC 協議》,轉載必須註明作者和本文連結
The sun is always behind the storm~