首页 >后端开发 >php教程 >php正则表达式处理方法

php正则表达式处理方法

小云云
小云云原创
2018-02-26 10:27:161998浏览

本文主要和大家分享两个php正则表达式处理方法,希望能帮助到大家。

1、处理汉字
例如:软件工程专业[1,2]  
获取:软件工程专业

public function doMobilealtermajor(){
            global $_W,$_GPC;            $_GPC[&#39;do&#39;]=&#39;altercode&#39;;            for($i=1;$i<=29083 ;$i++){                $info = M(&#39;enroll_major&#39;)->getInfo($i);                $major = $info[&#39;major&#39;];
                iconv("gbk","utf-8",$major);                $pattern = &#39;/([\x{4e00}-\x{9fa5}]+)(\[\d,\d\])/u&#39;;                $replace = &#39;$1&#39;;                $newmajor = preg_replace($pattern,$replace,$major);                $info[&#39;major&#39;] = $newmajor;
                M(&#39;enroll_major&#39;)->update($info);

            }            include $this->template(&#39;index&#39;);
    }

2、A001北京大学
分离出代码和文字

    public function doMobilealtername(){
        global $_W,$_GPC;        for($id=1;$id<=1225;$id++){            $info = M(&#39;enroll_science&#39;)->getInfo($id);            $pattern1 = &#39;/\w\d+([\S|\s]+)/i&#39;;            $replacement1 = &#39;$1&#39;;            $newname =  preg_replace($pattern1, $replacement1, $info[&#39;name&#39;]);            $pattern2 = &#39;/(\w\d+)([\S|\s]+)/i&#39;;            $replacement2 = &#39;$1&#39;;            $newcode =  preg_replace($pattern2, $replacement2,$info[&#39;name&#39;]);            $info[&#39;name&#39;] = $newname;            $info[&#39;zscode&#39;] = $newcode;

            M(&#39;enroll_science&#39;)->update($info);
        }            include $this->template(&#39;index&#39;);
    }

1、处理汉字
例如:软件工程专业[1,2]  
获取:软件工程专业

public function doMobilealtermajor(){
            global $_W,$_GPC;            $_GPC[&#39;do&#39;]=&#39;altercode&#39;;            for($i=1;$i<=29083 ;$i++){                $info = M(&#39;enroll_major&#39;)->getInfo($i);                $major = $info[&#39;major&#39;];
                iconv("gbk","utf-8",$major);                $pattern = &#39;/([\x{4e00}-\x{9fa5}]+)(\[\d,\d\])/u&#39;;                $replace = &#39;$1&#39;;                $newmajor = preg_replace($pattern,$replace,$major);                $info[&#39;major&#39;] = $newmajor;
                M(&#39;enroll_major&#39;)->update($info);

            }            include $this->template(&#39;index&#39;);
    }

2、A001北京大学
分离出代码和文字

    public function doMobilealtername(){
        global $_W,$_GPC;        for($id=1;$id<=1225;$id++){            $info = M(&#39;enroll_science&#39;)->getInfo($id);            $pattern1 = &#39;/\w\d+([\S|\s]+)/i&#39;;            $replacement1 = &#39;$1&#39;;            $newname =  preg_replace($pattern1, $replacement1, $info[&#39;name&#39;]);            $pattern2 = &#39;/(\w\d+)([\S|\s]+)/i&#39;;            $replacement2 = &#39;$1&#39;;            $newcode =  preg_replace($pattern2, $replacement2,$info[&#39;name&#39;]);            $info[&#39;name&#39;] = $newname;            $info[&#39;zscode&#39;] = $newcode;

            M(&#39;enroll_science&#39;)->update($info);
        }            include $this->template(&#39;index&#39;);
    }

相关推荐:

PHP正则表达式分组捕获实现方法

猪哥深谈php正则表达式及正则表达式都用在什么地方?

PHP正则表达式的基础及简单实例

以上是php正则表达式处理方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn