| 参数 | 描述 |
|---|---|
| separator | 必需。规定在哪里分割字符串。 |
| string | 必需。要分割的字符串。 |
| limit | 可选。规定所返回的数组元素的数目。 可能的值:
|
<?php// Example $fruit = "Apple Banana Orange Lemon Mango Pear";$fruitArray = explode(" ", $fruit);echo $fruitArray[]; // Appleecho $fruitArray[]; // Banana// Example $data = "gonn:*:nowamagic:::/home/foo:/bin/sh";list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data);echo $user; // gonnecho $pass; // *?>程序运行结果:
<?php$str = "one|two|three|four";// positive limitprint_r(explode("|", $str, ));// negative limit (since PHP .)print_r(explode("|", $str, -));?>程序运行结果:
Array([] => one[] => two|three|four)Array([] => one[] => two[] => three)Program List:将字符串化为键值数组
<?php// converts pure string into a trimmed keyed arrayfunction stringKeyedArray($string, $delimiter = ",", $kv = "=>") { if ($a = explode($delimiter, $string)) { // create partsforeach ($a as $s) { // each part if ($s) {if ($pos = strpos($s, $kv)) { // key/value delimiter $ka[trim(substr($s, , $pos))] = trim(substr($s, $pos + strlen($kv)));} else { // key delimiter not found $ka[] = trim($s);} }}return $ka; }} // stringKeyedArray$string = "a=>, b=>, $a, c=>%, true, d=>ab c";print_r(stringKeyedArray($string));?>程序运行结果:<?php $arr = array("Hello","World!","Beautiful","Day!"); echo implode(" ",$arr); ?>输出: