本文主要和大家分享兩個phpRegex處理方法,希望能協助到大家。
1、處理漢字
例如:軟體工程專業[1,2]
擷取:軟體工程專業
public function doMobilealtermajor(){ global $_W,$_GPC; $_GPC['do']='altercode'; for($i=1;$i<=29083 ;$i++){ $info = M('enroll_major')->getInfo($i); $major = $info['major']; iconv("gbk","utf-8",$major); $pattern = '/([\x{4e00}-\x{9fa5}]+)(\[\d,\d\])/u'; $replace = '$1'; $newmajor = preg_replace($pattern,$replace,$major); $info['major'] = $newmajor; M('enroll_major')->update($info); } include $this->template('index'); }
2、A001北京大學
分離出代碼和文字
public function doMobilealtername(){ global $_W,$_GPC; for($id=1;$id<=1225;$id++){ $info = M('enroll_science')->getInfo($id); $pattern1 = '/\w\d+([\S|\s]+)/i'; $replacement1 = '$1'; $newname = preg_replace($pattern1, $replacement1, $info['name']); $pattern2 = '/(\w\d+)([\S|\s]+)/i'; $replacement2 = '$1'; $newcode = preg_replace($pattern2, $replacement2,$info['name']); $info['name'] = $newname; $info['zscode'] = $newcode; M('enroll_science')->update($info); } include $this->template('index'); }
1、處理漢字
例如:軟體工程專業[1,2]
擷取:軟體工程專業
public function doMobilealtermajor(){ global $_W,$_GPC; $_GPC['do']='altercode'; for($i=1;$i<=29083 ;$i++){ $info = M('enroll_major')->getInfo($i); $major = $info['major']; iconv("gbk","utf-8",$major); $pattern = '/([\x{4e00}-\x{9fa5}]+)(\[\d,\d\])/u'; $replace = '$1'; $newmajor = preg_replace($pattern,$replace,$major); $info['major'] = $newmajor; M('enroll_major')->update($info); } include $this->template('index'); }
2、A001北京大學
分離出代碼和文字
public function doMobilealtername(){ global $_W,$_GPC; for($id=1;$id<=1225;$id++){ $info = M('enroll_science')->getInfo($id); $pattern1 = '/\w\d+([\S|\s]+)/i'; $replacement1 = '$1'; $newname = preg_replace($pattern1, $replacement1, $info['name']); $pattern2 = '/(\w\d+)([\S|\s]+)/i'; $replacement2 = '$1'; $newcode = preg_replace($pattern2, $replacement2,$info['name']); $info['name'] = $newname; $info['zscode'] = $newcode; M('enroll_science')->update($info); } include $this->template('index'); }