preg_replace()与preg_filter()

 1 <?php
 2 /*
 3     preg_replace($pattern, $replacement, $subject)
 4     preg_filter($pattern, $replacement, $subject)
 5 */
 6 header('content-type: text/html;charset = utf-8');
 7 
 8 //$pattern = '/[0-9]/';
 9 $pattern = array('/[0123]/', '/[456]/', '/[789]/');
10 //$subject = 'weuyr3ui76as83s0ck9';
11 $subject = array('weuy', 'r3ui', '76as83', 's', '0ck9');
12 //$replacement = '慕女神';
13 $replacement = array('慕', '女', '神');
14 
15 $str1 = preg_replace($pattern, $replacement, $subject);
16 $str2 = preg_filter($pattern, $replacement, $subject);
17 
18 show($str1);
19 echo '<hr />';
20 show($str2);
21 
22 // 对输出做个判断,选择合适的输出方式
23 function show($var = null){
24     if(empty($var)){
25         echo 'null';
26     }elseif(is_array($var) || is_object($var)){
27         //array, object
28         echo '<pre>';
29         print_r($var);
30         echo '</pre>';
31     }else{
32         //string, int, float...
33         echo $var;
34     }
35 }

 

posted @ 2017-07-14 16:21  有把刀  阅读(410)  评论(1编辑  收藏  举报