下面是根據explode()函數寫的切分分割字串的php函數,主要php按開始和結束截取中間資料,很實用
代碼如下:
<? // ### 切分字串 #### function jb51netcut($start,$end,$file){ $content=explode($start,$file); $content=explode($end,$content[1]); return $content[0]; } ?>
explode定義和用法
explode() 函數把字串分割為數組。
文法
explode(separator,string,limit)
| 參數 |
描述 |
| separator |
必需。規定在哪裡分割字串。 |
| string |
必需。要分割的字串。 |
| limit |
可選。規定所返回的數組元素的最大數目。 |
說明
本函數返回由字串組成的數組,其中的每個元素都是由 separator 作為邊界點分割出來的子字串。
separator 參數不能是Null 字元串。如果 separator 為空白字串(""),explode() 將返回 FALSE。如果 separator 所包含的值在 string 中找不到,那麼 explode() 將返回包含 string 中單個元素的數組。
如果設定了 limit 參數,則返回的數組包含最多 limit 個元素,而最後那個元素將包含 string 的剩餘部分。
如果 limit 參數是負數,則返回除了最後的 -limit 個元素外的所有元素。此特性是 PHP 5.1.0 中新增的。
提示和注釋
注釋:參數 limit 是在 PHP 4.0.1 中加入的。
注釋:由於曆史原因,雖然 implode() 可以接收兩種參數順序,但是 explode() 不行。你必須保證 separator 參數在 string 參數之前才行。
例子
在本例中,我們將把字串分割為數組:
代碼如下:
<?php $str = "Hello world. It's a beautiful day."; print_r (explode(" ",$str)); ?>
輸出:
Array
(
[0] => Hello
[1] => world.
[2] => It's
[3] => a
[4] => beautiful
[5] => day.
)