function str_replace_word($search, $replace, $subject, $_repl) {
$i=1;
$key = 1;
$kon = 0;
while ( $key ) {
$pos = stripos ( $subject, $search, $kon );
if ($pos !== FALSE) {
$len_word = strlen ( $replace );
if ($i==$_repl) {
$subject = substr_replace ( $subject, $replace, $pos, $len_word );
$key = 0;
} else {
$kon = $pos + $len_word;
$i ++;
}
} else {
$key = 0;
}
}
return $subject;
}
$text = "REPLACE
1text
2text
REPLACE
3text
REPLACE
4text";
echo str_replace_word('REPLACE', 'WHITH', $text, 2)