explode() 函式在 PHP 中
explode() 函式用於按字串分割字串。
語法
explode(delimiter, str, limit)
引數
分隔符 − 邊界字串
str − 要分割的字串
limit − 指定要返回的陣列元素的數量。
以下是可能的值 −
大於 0 - 返回一個最多包含 limit 個元素的陣列
小於 0 - 返回一個數組,但最後一個 -limit 元素除外
0 - 返回一個包含一個元素的陣列
返回
explode() 函式返回一個字串陣列。
以下是一個示例 −
示例
<?php
$s = "This is demo text!";
print_r (explode(" ",$s));
?>以下是輸出 −
輸出
Array ( [0] => This [1] => is [2] => demo [3] => text! )
讓我們看另一個示例 −
示例
<?php
$str = 'car,bus,motorbike,cycle';
print_r(explode(',',$str,0));
print "<br>";
?>以下是輸出 −
輸出
Array ( [0] => car,bus,motorbike,cycle )
讓我們看另一個示例 −
示例
<?php
$str = 'car,bus,motorbike,cycle';
print_r(explode(',',$str,2));
?>以下是輸出 −
輸出
Array ( [0] => car [1] => bus,motorbike,cycle )
讓我們看另一個示例 −
示例
<?php
$str = 'car,bus,motorbike,cycle';
print_r(explode(',',$str,-1));
?>以下是輸出 −
輸出
Array ( [0] => car [1] => bus [2] => motorbike )
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP