***實用函數:PHP explode()函數用法、切分字符串,作用,將字符串打散成數組


下面是根據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 參數不能是空字符串。如果 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. 
)

 


 

 

<!DOCTYPE html>
<html>
<body>

<?php
$str = 'one,two,three,four';

// 零 limit
print_r(explode(',',$str,0));

// 正的 limit
print_r(explode(',',$str,2));

// 負的 limit
print_r(explode(',',$str,-1));
?>

</body>
</html>

結果:

Array ( [0] => one,two,three,four ) Array ( [0] => one [1] => two,three,four ) Array ( [0] => one [1] => two [2] => three )

 

可以用來將以空格,逗號等分隔符連接的字符串一次性打散成一個數組!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM