Home  >  Article  >  Backend Development  >  PHP regular expression processing method

PHP regular expression processing method

小云云
小云云Original
2018-02-26 10:27:161960browse

本文主要和大家分享两个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正则表达式的基础及简单实例

The above is the detailed content of PHP regular expression processing method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn