日期:2014-05-17 浏览次数:20575 次
split_array("百度#http://www.baidu.com/\n百度#http://www.baidu.com/\n","\n|#","<a href='{1}'>{0}</a>");
function split_array($str, $pattern, $template) {
$cnt = substr_count($template, '{'); //统计模板中替换位置的个数
$tmp = preg_split('/'.$pattern.'/', $str, -1, PREG_SPLIT_NO_EMPTY); //按规则切割数据
$m = count($tmp) % $cnt;
if($m) $tmp = array_slice($tmp, 0, -$m); //除去多余的数据
$tmp = array_chunk($tmp, $cnt); //将数据分组
$r = array();
foreach($tmp as $m) {
$t = $template;
foreach($m as $i=>$v) {
$t = str_replace("{{$i}}", $v, $t);
}
$r[] = $t;
}
echo join('<br>', $r);
}