首頁 >後端開發 >php教程 >(PHP)正規表示式-preg_replace()和preg_filter()

(PHP)正規表示式-preg_replace()和preg_filter()

黄舟
黄舟原創
2016-12-30 11:04:411728瀏覽

<?php
/**
 * 正则表达式练习
 * User: Ollydebug
 * Date: 2015/11/13
 * Time: 13:28
 */

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

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

show($str1);
echo &#39;<hr/>&#39;;
show($str2);

function show($var){
    if(empty($var)){
        echo &#39;null&#39;;
    }elseif(is_array($var)||is_object($var)){
        // array,object
        echo &#39;<pre class="brush:php;toolbar:false">&#39;;
        print_r($var);
        echo &#39;
'; }else{ //string,int,float echo $var; } } ?>

 以上就是(PHP)正規表示式-preg_replace()和preg_filter()的內容,更多相關內容請關注PHP中文網(www.php.cn)!


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn