0

ここに、4chanスタイルの引用のすべてのインスタンス(たとえば、> 10、> 59、> 654564)を検出し、それをパターン出力として返す正規表現があります。私の質問は、私のパターン出力を挿入することは可能ですか...

\ 1

...PHP関数に。

これが正常に機能している間:

$a = preg_replace('`(>\d+)`i', '\1', $b);

私が探しているものはそうではありません:

$a = preg_replace('`(>\d+)`i', '".getpost('\1')."', $b);
4

1 に答える 1

2

preg_replace_callback()関数を見てください。


例[php>=5.3.0]Closureを使用):

$callback = function($match) {
    return "{" . $match[1] . "}"; # do smth with match
};
$string = 'test1 >1 test2 >12 test3 >123 test4';
echo preg_replace_callback('~(>\d+)~i', $callback, $string);

出力します:

test1 {>1} test2 {>12} test3 {>123} test4

例[php<5.3.0]

function replaceCallback($match) {
    return "{" . $match[1] . "}"; # do smth with match
};
$string = 'test1 &gt;1 test2 &gt;12 test3 &gt;123 test4';
echo preg_replace_callback('~(&gt;\d+)~i', 'replaceCallback', $string);
于 2013-01-15T20:09:45.663 に答える