1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
/* ---------------------------------------
 *  正規表現のpreg_replace_callbackの例
 *
 *  作成: 2015.07.23 yoshi of CXMedia Inc.
 * --------------------------------------- */
header("Content-type: text/plain; charset=UTF-8");

// 変換対象のpタグの文字列
$data    '<p class="ab">楽しい</p>';
// HTMLタグの正規表現パターン
$pattern '/<([^\s]*)\s+(?:class=\"([^"]*)\")>(.+?[^<])<\/([^>]*)>/';
// tag_change関数をcallbackした置換
$out1    preg_replace_callback($pattern,"tag_change",$data);
echo 
$out1,"\n";

/* -----------------------------------
 *   HTMLタグの編集、ユーザ関数利用 
 * ----------------------------------- */
function tag_change($rst){
    
print_r($rst);
    
//開始タグと終了タグが一致した場合、h1タグに変更
    
if($rst[1] == $rst[4]){
        
$wk '<h1 class="'.$rst[2].'">'.$rst[3].'</h1>';
    } else {
        echo 
"error RegPattern";
    }
    return 
$wk;
}
?>