explode() - 把字符串轉化成數組;
implode() - 把數組轉化成字符串;
explode()
把字符串按指定的字符分割成數組返回;
基礎語法:
array explode(str $delimiter ,str $string [,int $limit]);
array - 返回的數組;
str - 用來分割的字符;
limit - 限定符,規定返回數組的長度;
語法結構1:
array explode(str $delimiter ,str $string);
array - 返回的數組;
delimiter - 分割的字符;當 delimiter 匹配 string 中的字符的時候 - 返回能夠得到的最大長度的數組;
當 delimiter 沒有匹配 string 中的字符的時候 - 返回整個字符串為單一元素的數組;
當 delimiter == null 的時候 - 彈出警告;
實例:
$str_1 = 'this is a dog !';
print_r(explode(' ',$str_1));
#output : Array ( [0] => this [1] => is [2] => a [3] => dog [4] => ! )
print_r(explode('e',$str_1));
#output : Array ( [0] => this is a dog ! )
print_r(explode(null ,$str_1));
#output : Warning: explode(): Empty delimiter in ......
語法結構2:
array explode(str $delimiter ,str $string ,int $limit);
array - 返回的數組;
delimiter - 用來分割的字符;當 delimiter 有匹配 string 中的字符的時候;
當 limit > 0 的時候 - 返回指定長度的數組,如果指定的長度 小于 能夠返回的最大長度數組的長度,那么多余的子串,全部包含在數組的最后一個元素內;
當 limit = 0 的時候 - 返回整個字符串為單一數組元素的數組;
當 limit < 0 的時候 - 返回 最大長度的數組從末尾開始 減去 limit 個元素 后的數組;
當 delimiter 沒有 匹配string 中的字符的時候;
當 limit >= 0 返回 整個字符串為單一元素的數組;
當 limit < 0 返回空數組;
limit - 限定符,規定返回數組的長度;
實例:
$str_1 = 'this is a dog !';
print_r(explode(' ',$str_1,0));
#output :Array ( [0] => this is a dog ! )
print_r(explode(' ',$str_1,2));
#output :Array ( [0] => this [1] => is a dog ! )
print_r(explode(' ',$str_1,30));
#output : Array ( [0] => this [1] => is [2] => a [3] => dog [4] => ! )
print_r(explode(' ',$str_1,-3));
#output : Array ( [0] => this [1] => is )
print_r(explode('e',$str_1,2));
#output :Array ( [0] => this is a dog ! )
print_r(explode('e',$str_1,-1));
#output : Array ( )
implode()
把數組轉化成字符串;
基礎語法:
string implode(str $connect ,array $array);
string - 返回的字符串;
connect - 連接符;
array - 被操作的數組;
實例:
$a_1 = ['this','is','a','dog','!'];
print_r(implode(' ',$a_1));
#output :this is a dog !