全部计数<;br/>;并用php替换数字4
我有像这样的文字全部计数<;br/>;并用php替换数字4,php,replace,Php,Replace,我有像这样的文字 "Hello, Hello, how are you? Hello, Hello, how are you?<br /> Hello, Hello,<br /> How are you? How are you today? I am fine; I am great I am fine;<br /> I am just great I am fine; I am great I'm very well today!<br /
"Hello, Hello, how are you?
Hello, Hello, how are you?<br />
Hello,
Hello,<br />
How are you?
How are you today?
I am fine; I am great
I am fine;<br />
I am just great
I am fine; I am great
I'm very well today!<br />
Great!
Hello, Hello, how are you?<br />
Hello, Hello, how are you?
Hello, Hello, How are you?<br />
How are you today?"
但这是唯一一个找到的
我尝试替换数字4尝试以下操作:
<?php
$txt = "Hello, Hello, how are you?
Hello, Hello, how are you?<br />
Hello,
Hello,<br />
How are you?
How are you today?
I am fine; I am great
I am fine;<br />
I am just great
I am fine; I am great
I'm very well today!<br />
Great!
Hello, Hello, how are you?<br />
Hello, Hello, how are you?
Hello, Hello, How are you?<br />
How are you today?";
$occurrences = substr_count($txt, '<br />');
if( 1 === $occurrences ) {
$txt = str_replace('<br />', "<img>", $txt);
} else {
$counter = 1;
$txt = preg_replace_callback("/<br \/>/", function ($m) use (&$counter) {
// replace 4th occurance
if ($counter++ == 4) {
return "<img>";
}
return $m[0];
}, $txt );
}
echo $txt;
StackOverflow不是免费的编码服务。你应该会的。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。有关更多信息,请参阅,并点击:)@Barmar谢谢您的评论,好的,我添加它。您可能需要使用PHP REgeXi来添加类似的内容$发生次数=子项计数($stwwr,
)$justit=ceil($2/2)-1;来代替它的中间部分
<?php
$txt = "Hello, Hello, how are you?
Hello, Hello, how are you?<br />
Hello,
Hello,<br />
How are you?
How are you today?
I am fine; I am great
I am fine;<br />
I am just great
I am fine; I am great
I'm very well today!<br />
Great!
Hello, Hello, how are you?<br />
Hello, Hello, how are you?
Hello, Hello, How are you?<br />
How are you today?";
$occurrences = substr_count($txt, '<br />');
if( 1 === $occurrences ) {
$txt = str_replace('<br />', "<img>", $txt);
} else {
$counter = 1;
$txt = preg_replace_callback("/<br \/>/", function ($m) use (&$counter) {
// replace 4th occurance
if ($counter++ == 4) {
return "<img>";
}
return $m[0];
}, $txt );
}
echo $txt;