1 回答
TA贡献1784条经验 获得超9个赞
尝试这个 :
<?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;
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报