當擁有一個龐大的字串時,運用explode和str_split進行整理。
explode功能是甚麼?如何使用?
介紹:explode(string $separator, string $string, int $limit = PHP_INT_MAX): array
$string:你想要分割的字串
$separator:原本的字串
$limit:最多分成幾份;這一個變數特別的是如果limit是負數,則不會輸出最後一個變數。
如果是0則會被當成1。
//範例
<?php
$str = 'one|two|three|four';
print_r(explode('|', $str, 2));
print_r(explode('|', $str, -1));
?>
//輸出
Array
(
    [0] => one
    [1] => two|three|four
)
Array
(
    [0] => one
    [1] => two
    [2] => three
)
str_split功能是甚麼?如何使用?
介紹:str_split(string $string, int $length = 1): array
$string:要切分的資料
$length:每個資料長度多少
//範例
<?php
$str = "Hello world";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
?>
//輸出
Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => w
    [7] => o
    [8] => r
    [9] => l
    [10] => d
)
Array
(
    [0] => Hel
    [1] => lo
    [2] => wor
    [3] => ld
)
更多方法:
- preg_split() - Split string by a regular expression
 - mb_split() - Split multibyte string using regular expression
 - str_word_count() - Return information about words used in a string
 - strtok() - Tokenize string
 - implode() - Join array elements with a string