我用过的一段php代码。。
function str_replace_nth($search, $replace, $subject, $nth){
$match_arr = explode($search,$subject);
$match_arr = array_filter($match_arr);
foreach($match_arr as $key=>$val){
if($key == $nth-1){ // array index start with Zero
$match_arr[$key] .= $replace;
}else{
$match_arr[$key] .= '?';
}
}
return implode('',$match_arr);
} |