public function parse_form($template,$fields=0)
{
//获取标签
//$preg = "/<(img|input|textarea|select).*?(<\/select>|<\/textarea>|\/>)/s";
/*
//正则 radios|checkboxs|select
js 匹配的边界 |--| 因为当使用 {} 时js报错
php 要保持一致,也使用 |--| 也可以是直接 {|- -|}
*/
$preg = "/(\|-<span(((?!<span).)*leipiplugins=\"(radios|checkboxs|select)\".*?)>(.*?)<\/span>-\||<(img|input|textarea|select).*?(<\/select>|<\/textarea>|\/>))/s";
//获取属性 修改为可变 的匹配
$preg_attr ="/(\w+)=\"(.?|.+?)\"/s";
//获取单选组复选
$preg_group ="/<input.*?\/>/s";
$template_parse =$template;
preg_match_all($preg,$template,$temparr);
$template_data = $add_fields = array();
$checkboxs =0;
if($temparr)
{
foreach($temparr[0] as $pno=>$plugin)
{
$tag = empty($temparr[6][$pno]) ? $temparr[4][$pno] : $temparr[6][$pno];
$attr_arr_all = array();
$name = $leipiplugins = $select_dot = '';
$is_new=false;
if(in_array($tag,array('radios','checkboxs')))//这两个在下面也要//清空边界
{
$plugin = $temparr[2][$pno];
}else if($tag =='select')
{
$plugin = str_replace(array('|-','-|'),'',$plugin);//清空边界
}
preg_match_all($preg_attr,$plugin,$parse_attr);
foreach($parse_attr[1] as $k=>$attr)
{
$attr = trim($attr);
if($attr)
{
$val = $parse_attr[2][$k];
if($attr=='name')
{
if($val=='leipiNewField')
{
$is_new=true;
$fields++;
$val = 'data_'.$fields;
}
$name = $val;
}
if($tag=='select' && $attr=='value')
{
$attr_arr_all[$attr] .= $select_dot . $val;
$select_dot = ',';
}else
{
$attr_arr_all[$attr] = $val;
}
}
}
if($tag =='checkboxs') /*复选组 多个字段 */
{
$plugin = $temparr[0][$pno];
$plugin = str_replace(array('|-','-|'),'',$plugin);//清空边界
$name = 'checkboxs_'.$checkboxs;
$attr_arr_all['parse_name'] = $name;
$attr_arr_all['name'] = '';
$attr_arr_all['value'] = '';
$options = $temparr[5][$pno];
preg_match_all($preg_group,$options,$parse_group);
$dot_name = $dot_value = '';
$attr_arr_all['content'] = '<span leipiplugins="checkboxs" title="'.$attr_arr_all['title'].'">';
foreach($parse_group[0] as $value)
{
preg_match_all($preg_attr,$value,$parse_attr);
$is_new=false;
$option = array();
foreach($parse_attr[1] as $k=>$val)
{
$tmp_val = $parse_attr[2][$k];
if($val=='name')
{
if($tmp_val=='leipiNewField')
{
$is_new=true;
$fields++;
$tmp_val = 'data_'.$fields;
}
$attr_arr_all['name'] .= $dot_name . $tmp_val;
$dot_name = ',';
}else if($val=='value')
{
$attr_arr_all['value'] .= $dot_value . $tmp_val;
$dot_value = ',';
}
$option[$val] = $tmp_val;
}
$attr_arr_all['options'][] = $option;
if($is_new)
{
$add_fields[$option['name']] = array(
'name'=>$option['name'],
'leipiplugins'=>$attr_arr_all['leipiplugins']
);
}
$checked = $option['checked'] ? 'checked="checked"' : '';
$attr_arr_all['content'] .= '<input type="checkbox" name="'.$option['name'].'" value="'.$option['value'].'" '.$checked.'/>'.$option['value'].' ';
}
$attr_arr_all['content'] .= '</span>'; //需要content replace
//parse
$template = self::str_replace_once($plugin,$attr_arr_all['content'],$template);
$template_parse = self::str_replace_once($plugin,'{'.$name.'}',$template_parse);
$template_parse = str_replace(array('{|-','-|}'),'',$template_parse);//清空边界
$template_data[$pno] = $attr_arr_all;
$checkboxs++;
}
else if($name)
{
if($tag =='radios') /*单选组 一个字段*/
{
$plugin = $temparr[0][$pno];
$plugin = str_replace(array('|-','-|'),'',$plugin);//清空边界
$attr_arr_all['value'] = '';
$options = $temparr[5][$pno];
preg_match_all($preg_group,$options,$parse_group);
$dot = '';
$attr_arr_all['content'] = '<span leipiplugins="radios" name="'.$attr_arr_all['name'].'" title="'.$attr_arr_all['title'].'">';
foreach($parse_group[0] as $value)
{
preg_match_all($preg_attr,$value,$parse_attr);
$option = array();
foreach($parse_attr[1] as $k=>$val)
{
if($val=='value')
{
$attr_arr_all['value'] .= $dot . $parse_attr[2][$k];
$dot = ',';
}
$option[$val] = $parse_attr[2][$k];
}
$option['name'] = $attr_arr_all['name'];
$attr_arr_all['options'][] = $option;
$checked = $option['checked'] ? 'checked="checked"' : '';
$attr_arr_all['content'] .='<input type="radio" name="'.$attr_arr_all['name'].'" value="'.$option['value'].'" '.$checked .'/>'.$option['value'].' ';
}
$attr_arr_all['content'] .= '</span>'; //需要content replace
}else
{
$attr_arr_all['content'] = $is_new ? str_replace('leipiNewField',$name,$plugin) : $plugin;
}
$template = self::str_replace_once($plugin,$attr_arr_all['content'],$template);
$template_parse = self::str_replace_once($plugin,'{'.$name.'}',$template_parse);
$template_parse = str_replace(array('{|-','-|}'),'',$template_parse);//清空边界
if($is_new)
{
$add_fields[$name] = array(
'name'=>$name,
'leipiplugins'=>$attr_arr_all['leipiplugins']
);
}
$template_data[$pno] = $attr_arr_all;
}
}
}
$parse_form = array(
'fields'=>$fields,
'template'=>$template,
'parse'=>$template_parse,
'data'=>$template_data,
'add_fields'=>$add_fields,
);
//print_r($parse_form);exit;
return $parse_form;
}
这个有点费时…
这个好麻烦,要实现什么功能,在c#下重写吧
'leipiplugins'=>$attr_arr_all['leipiplugins']
累屁plugins
自己解决了,累
C#版本:https://www.cnblogs.com/luomingui/p/18378988