阅读:2324回复:0
理解PHP正则替换方法
理解PHP正则替换方法
preg_filter() preg_replace() $pattern = array('/d/', '/[a-z]/', '/[1a]/','/[abc]/'); $replace = array('A:$0[', 'B+$0]', 'C-$0{','D=$0}'); //$replace = 'G'; $subject = array('1abc', 'a', '2', 'b', '3', 'Zac', 'X', '4'); echo "preg_filter returnsn"; var_dump(preg_filter($pattern, $replace, $subject)); echo "preg_replace returnsn"; var_dump(preg_replace($pattern, $replace, $subject)); //输出结果 array (size=7) 0 => string 'A:C-1{[B+C-D=a}{]B+D=b}]B+D=c}]' (length=31) 1 => string 'B+C-D=a}{]' (length=10) 2 => string 'A:2[' (length=4) 3 => string 'B+D=b}]' (length=7) 4 => string 'A:3[' (length=4) 5 => string 'ZB+C-D=a}{]B+D=c}]' (length=18) 7 => string 'A:4[' (length=4) preg_replace returns array (size=8) 0 => string 'A:C-1{[B+C-D=a}{]B+D=b}]B+D=c}]' (length=31) 1 => string 'B+C-D=a}{]' (length=10) 2 => string 'A:2[' (length=4) 3 => string 'B+D=b}]' (length=7) 4 => string 'A:3[' (length=4) 5 => string 'ZB+C-D=a}{]B+D=c}]' (length=18) 6 => string 'X' (length=1) 7 => string 'A:4[' (length=4) |
|