PHP 输出 pre_replace_callback
PHP Output pre_replace_callback
我是php编码新手,我想输出第一个和第三个匹配项。下面的代码是我目前的编码。
$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][1];
以获得第一个匹配项,输出 $rtt=$submatch[0][3];
以获得第三个匹配项,但不起作用。
预期结果;
//This is first text
This is the third text
您没有使用 $matchcount
来测试它是哪个匹配项。
此外,匹配 \r
和 \n
不会匹配最后一行的结尾,因为它们之后没有换行符。您还需要匹配 $
,这是字符串的结尾。
([^\[nextpage\]]*)
完全没有必要,并且不会像您认为的那样。 [^string]
并不意味着不匹配该字符串,它匹配任何不是这些字符之一的单个字符。
$string = preg_replace_callback("/\[nextpage\]([^\r\n]*)(\n|\r\n?|$)/is", function ($submatch) {
static $matchcount = 0;
$matchcount++;
if ($matchcount == 1 || $matchcount == 3) {
$rtt=$submatch[1];
return "<li>$rtt</li>";
} else {
return "";
}
}, $string);
也许需要采用不同的方法?
您可以将字符串拆分成一个数组,然后选择您关心的部分。
<?php
$string="[nextpage] This is first text
[nextpage] This is the second text
[nextpage] This is the third text";
$explosion = explode('[nextpage] ', $string);
var_dump($explosion);
$textICareAbout = trim($explosion[1]) . " " . trim($explosion[3]);
echo $textICareAbout;
产量
array(4) {
[0]=>
string(0) ""
[1]=>
string(20) "This is first text
"
[2]=>
string(25) "This is the second text
"
[3]=>
string(22) "This is the third text"
}
This is first text This is the third text
我是php编码新手,我想输出第一个和第三个匹配项。下面的代码是我目前的编码。
$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][1];
以获得第一个匹配项,输出 $rtt=$submatch[0][3];
以获得第三个匹配项,但不起作用。
预期结果;
//This is first text
This is the third text
您没有使用 $matchcount
来测试它是哪个匹配项。
此外,匹配 \r
和 \n
不会匹配最后一行的结尾,因为它们之后没有换行符。您还需要匹配 $
,这是字符串的结尾。
([^\[nextpage\]]*)
完全没有必要,并且不会像您认为的那样。 [^string]
并不意味着不匹配该字符串,它匹配任何不是这些字符之一的单个字符。
$string = preg_replace_callback("/\[nextpage\]([^\r\n]*)(\n|\r\n?|$)/is", function ($submatch) {
static $matchcount = 0;
$matchcount++;
if ($matchcount == 1 || $matchcount == 3) {
$rtt=$submatch[1];
return "<li>$rtt</li>";
} else {
return "";
}
}, $string);
也许需要采用不同的方法?
您可以将字符串拆分成一个数组,然后选择您关心的部分。
<?php
$string="[nextpage] This is first text
[nextpage] This is the second text
[nextpage] This is the third text";
$explosion = explode('[nextpage] ', $string);
var_dump($explosion);
$textICareAbout = trim($explosion[1]) . " " . trim($explosion[3]);
echo $textICareAbout;
产量
array(4) {
[0]=>
string(0) ""
[1]=>
string(20) "This is first text
"
[2]=>
string(25) "This is the second text
"
[3]=>
string(22) "This is the third text"
}
This is first text This is the third text