Ну попробуй этот кусок:
$text = file_get_contents('file.txt');
$words = explode(PHP_EOL, file_get_contents('words.txt'));
array_pop($words);
$to_replace = '123';
$current_word_idx = 0;
$pos = 0;
while($pos = mb_strpos($text, $to_replace, $pos, 'UTF-8')) {
$text = mb_substr_replace($text, $words[$current_word_idx ], $pos, mb_strlen($words[$current_word_idx ], 'UTF-8'));
$current_word_idx = ++$current_word_idx % count($words);
}
echo $text;
function mb_substr_replace($string, $replacement, $start, $length=NULL) {
if (is_array($string)) {
$num = count($string);
$replacement = is_array($replacement) ? array_slice($replacement, 0, $num) : array_pad(array($replacement), $num, $replacement);
if (is_array($start)) {
$start = array_slice($start, 0, $num);
foreach ($start as $key => $value)
$start[$key] = is_int($value) ? $value : 0;
}
else {
$start = array_pad(array($start), $num, $start);
}
if (!isset($length)) {
$length = array_fill(0, $num, 0);
}
elseif (is_array($length)) {
$length = array_slice($length, 0, $num);
foreach ($length as $key => $value)
$length[$key] = isset($value) ? (is_int($value) ? $value : $num) : 0;
}
else {
$length = array_pad(array($length), $num, $length);
}
return array_map(__FUNCTION__, $string, $replacement, $start, $length);
}
preg_match_all('/./us', (string)$string, $smatches);
preg_match_all('/./us', (string)$replacement, $rmatches);
if ($length === NULL) $length = mb_strlen($string);
array_splice($smatches[0], $start, $length, $rmatches[0]);
return join($smatches[0]);
}
Proof of concept:
http://ideone.com/XlCJB4