时间:2021-07-01 10:21:17 帮助过:16人阅读
split_array("百度#http://www.baidu.com/\n百度#http://www.baidu.com/\n","\n|#","{0}"); 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('
', $r); }