PHP中explode()与implode()函数用法
时间:2015-06-10 阅读:次 QQ群:182913345
1、explode:使用一个字符串分割另一个字符串
语法:array explode(string $separator,string $string [,int $limit ])
<?php /* 示例 1 */ $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 /* 示例 2 */ $str = 'one|two|three|four'; // 正数的 limit print_r(explode('|', $str, 2)); // 负数的 limit(自 PHP 5.1 起) print_r(explode('|', $str, -1)); /* * 以上例程会输出: * Array ( [0] => one [1] => two|three|four ) Array ( [0] => one [1] => two [2] => three ) */ |
2、implode:将数组变成字符串
语法:string implode(string $glue,array $pieces)
<?php /* 示例 */ $array = array('lastname', 'email', 'phone'); $comma_separated = implode(",", $array); echo $comma_separated; // lastname,email,phone |
上一篇:php.ini配置详解