私は初心者をコードするPHPですので、最初と3番目のマッチを出力したいと思います。以下のコードはこれまでのコーディングです。PHPの出力pre_replace_callback
$string="[nextpage] This is first text
[nextpage] This is the second text
[nextpage] This is the third text";
$string = preg_replace_callback("/\[nextpage\]([^\[nextpage\]]*)(.*?)(\n|\r\n?)/is", function ($submatch) {
static $matchcount = 0;
$matchcount++;
$rtt=$submatch[2];
return "<li>$rtt</li>";
}, $string);
echo $string; //This is first text
This is the second text
This is the third text
私は、第三試合を得るために、最初に一致し、$rtt=$submatch[0][3];
を取得するために$rtt=$submatch[0][1];
を出力しようとしましたが、動作しません。
予想される結果。
//This is first text
This is the third text
感謝を生み出します。これは私の問題を解決しました。 – Omotayo
@barmarどうすれば 'count($ submatch)'を試した正規表現の総マッチを得ることができますが動作しません。 – Omotayo
'$ matchcount'をグローバル変数にすることができます。それは、マッチの数を含みます。 – Barmar