首頁  >  文章  >  後端開發  >  學習php中利用explode函數分割字串到陣列

學習php中利用explode函數分割字串到陣列

coldplay.xixi
coldplay.xixi轉載
2020-07-27 16:55:102227瀏覽

學習php中利用explode函數分割字串到陣列

分割字串

//利用explode 函數分割字串到陣列

程式碼如下:

<?php 
$source = "hello1,hello2,hello3,hello4,hello5";//按逗号分离字符串 
$hello = explode(&#39;,&#39;,$source); 
for($index=0;$index<count($hello);$index++) 
{ 
echo $hello[$index];echo "</br>"; 
} 
?> 
//split函数进行字符分割 
// 分隔符可以是斜线,点,或横线

程式碼如下:

<?php 
$date = "04/30/1973"; 
list($month, $day, $year) = split (&#39;[/.-]&#39;, $date); 
echo "Month: $month; Day: $day; Year: $year<br />\n"; 
?>

透過陣列實作多條件查詢的程式碼

程式碼如下:

<?php
$keyword="asp php,jsp";
$keyword=str_replace("  "," ",$keyword);
$keyword=str_replace(" ",",",$keyword);
$keyarr=explode(&#39;,&#39;,$keyword); 
for($index=0;$index<count($keyarr);$index++) 
{ 
$whereSql .= " And (arc.title like &#39;%$keyarr[$index]%&#39; Or arc.keywords like &#39;%$keyarr[$index]%&#39;) ";
} 
echo $whereSql;

相關學習推薦:PHP程式設計從入門到精通

以上是學習php中利用explode函數分割字串到陣列的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jb51.net。如有侵權,請聯絡admin@php.cn刪除