执行以下操作的最佳方法是什么?
# if I have the following string :
$str = "Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar";
# and I want to remove "o" character after 5-th match so result I need is :
$newStr = "Foo Bar Foo Bar Fo Bar F Bar F Bar F Bar F Bar...";
我知道的方式:
$str = "Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar";
$str = explode("o", $str);
$new = "";
$c = 1;
foreach($str as $k) {
if($c>5)
$new .= $k;
else
$new .= $k."o";
$c++;
}
我相信有更好的方法来做到这一点。