Rumah >pembangunan bahagian belakang >tutorial php >分析PHP函数explode如何将字符串转换为数组_PHP教程
我们在学习1.PHP函数explode的格式如下:
array explode(string separator, string string,[int limit]);
2.参数中的separator为分隔符,string为待分割的字符串,limit为返回的数组中元素的最大个数。
PHP函数explode的具体使用实例:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">DOCUMENT_ROOT</font></span><span> = $_SERVER['DOCUMENT_ROOT']; </span> </li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"><span class="tag-name">html</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">head</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">title</span><span class="tag">></span></span></font></strong><span>Test Explode</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">title</span><span class="tag">></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">head</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span> </span><strong><font color="#006699"><span class="tag"><span class="tag-name">body</span><span class="tag">></span></span></font></strong><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">peoples</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">file</font></span><span>($DOCUMENT_ROOT.'/test/03/people.txt'); </span> </li> <li class="alt"> <span> $</span><span class="attribute"><font color="#ff0000">count</font></span><span class="attribute-value"><font color="#0000ff">count</font></span><span> = count($peoples); </span> </li> <li class=""> <span> if ($</span><span class="attribute"><font color="#ff0000">count</font></span><span> == 0) { </span> </li> <li class="alt"><span> echo 'NO peoples pending. Please try again later.'; </span></li> <li class=""><span> } </span></li> <li class="alt"> <span> for ($</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i </span><span class="tag"><strong><font color="#006699"></font></strong></span><span> $count; $i++) { </span> </li> <li class=""> <span> $</span><span class="attribute"><font color="#ff0000">line</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">explode</font></span><span>("t", $peoples[$i]); </span> </li> <li class="alt"> <span> echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">font</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">color</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">red</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'.$line[0].'</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">font</span><span class="tag">></span></font></strong><span>'; </span> </li> <li class=""> <span> echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">font</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">color</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">blue</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'.$line[1].'</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">font</span><span class="tag">></span></font></strong><span>'; </span> </li> <li class="alt"> <span> echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">font</span></span></font></strong><span> </span><span class="attribute"><font color="#ff0000">color</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">green</font></span><span class="tag"><strong><font color="#006699">></font></strong></span><span>'.$line[2].'</span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">font</span><span class="tag">></span></font></strong><span>'; </span> </li> <li class=""> <span> echo '</span><strong><font color="#006699"><span class="tag"><span class="tag-name">br</span></span></font></strong><span> </span><span class="tag"><strong><font color="#006699">/></font></strong></span><span>'; </span> </li> <li class="alt"><span> } </span></li> <li class=""> <span> </span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> <li class="alt"> <span> </span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">body</span><span class="tag">></span></font></strong><span> </span> </li> <li class=""> <span></span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">html</span><span class="tag">></span></font></strong><span> </span> </li> </ol>
希望广大学习爱好者们通过本文所介绍的PHP函数explode的使用示例,能够基本弄清PHP函数explode的使用方法。