1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
/* -----------------------------------------------
* 正規表現:reg_filter() PHP5.3以降
* 【preg_filter() と preg_replace() の比較】
* -- reg_filter()は、マッチした結果(を変換したもの)のみを返す
*
* -- reg_filter()は、見つからなかったりエラーが発生したりした場合は、
* subjectが array なら空の配列を返し、そうでなければ NULL を返します。 --
* ------
* 作成: 2015.07.18 yoshi of CXMedia Inc.
* ----------------------------------------------- */
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');
header("Content-type:text/plain; charset=UTF-8");
echo "preg_filter の実行結果\n";
print_r(preg_filter($pattern, $replace, $subject));
echo "preg_replace の実行結果\n";
print_r(preg_replace($pattern, $replace, $subject));
?>