欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

(PHP)正则表达式-preg_replace()和preg_filter()

程序员文章站 2022-03-29 22:17:32
...
<?php
/**
 * 正则表达式练习
 * User: Ollydebug
 * Date: 2015/11/13
 * Time: 13:28
 */

/*
 * preg_replace()不论发不发生替换,值都保留.
 * preg_filter()只保留发生替换的值.
 */

$pattern = array('/[0123]/','/[456]/','/[789]/');
$subject = array("weuy","r3ui","76as83","s","0ck9");
$replacement = array('百','小','度');
$str1 = preg_replace($pattern,$replacement,$subject);
$str2 = preg_filter($pattern,$replacement,$subject);

show($str1);
echo '<hr/>';
show($str2);

function show($var){
    if(empty($var)){
        echo 'null';
    }elseif(is_array($var)||is_object($var)){
        // array,object
        echo '<pre>';
        print_r($var);
        echo '</pre>';
    }else{
        //string,int,float
        echo $var;
    }
}

?>

以上就是(PHP)正则表达式-preg_replace()和preg_filter()的内容,更多相关内容请关注PHP中文网(www.php.cn)!