特定のパターンを増分カウント数に置き換える必要があります。私のコード:
$text = "Hello (apple) hello (banana) hello (cucumber)";
$pattern = '/\(([^\)]*)\)/s';
$i = 0;
$returnText = preg_replace_callback($pattern, function($matches) use ($i) {
return '<sup>['.$i++.']</sup>';
}, $text);
echo $returnText;
結果:
Hello [0] hello [0] hello [0]
必要なもの:
Hello [0] hello [1] hello [2]
私は何を間違っていますか?