name文本框,以非文字字符分割,name中文转拼音到nickname字段中
一、修改文件appadminlibrarytraitsBackend.php;
1、加入:use fastPinyin;
语句
2、修改add()
方法
/** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\model\", "\validate\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validate($validate); } //或批量添加 if(isset($params['name'])){ $name2 = strip_tags($params['name']); $name2 = preg_replace( '/[s]+/u', ',', $name2); if($name2 === $params['name']) {//不含有非字母数字字符 $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } }else{//批量添加 $name_array = explode(',', $name2); $name_array = array_reverse(array_unique($name_array)); //去重复,倒排 $py = new Pinyin(); $list = []; foreach ($name_array as $name2){ $params['name'] = $name2; //判断是否有中文,中文转字母 $isChinese = true; if($isChinese){ $params['nickname'] = $py->get($name2); //中文对应的字母,存在别名中 $params['name'] = $name2; } $list[] = $params; } $result = $this->model->saveAll($list, false); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } }else{ $result = $this->model->allowField(true)->save($params); if ($result !== false) { $this->success(); } else { $this->error($this->model->getError()); } } } catch (thinkexceptionPDOException $e) { $this->error($e->getMessage()); } catch (thinkException $e) { $this->error($e->getMessage()); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); }
二、在表格中加入nickname字段
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。