PHP explode() 函式
示例
將字串分割為陣列
<?php
$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str));
?>
自己動手試一試 »
定義和用法
explode() 函式將字串分割為陣列。
注意: "separator" 引數不能為空字串。
注意:此函式是二進位制安全的。
語法
explode(separator,string,limit)
引數值
引數 | 描述 |
---|---|
separator | 必需。規定在何處分割字串 |
string | 必需。要分割的字串 |
limit | 可選。規定返回的陣列元素的數量。 可能的值
|
技術詳情
返回值 | 返回一個字串陣列 |
---|---|
PHP 版本 | 4+ |
更新日誌 | limit 引數在 PHP 4.0.1 中新增,對負數 limit 的支援在 PHP 5.1.0 中新增。 |
更多示例
示例
使用 limit 引數返回一定數量的陣列元素
<?php
$str = 'one,two,three,four';
// limit 為零
print_r(explode(',',$str,0));
// 正數 limit
print_r(explode(',',$str,2));
// 負數 limit
print_r(explode(',',$str,-1));
?>
自己動手試一試 »
❮ PHP 字串參考