Brief
Hello. I just can not get the result I want. So I divided the problem into pieces to solve. If of course someone will decide. If in general this is possible. I have text and three arrays. It is necessary to replace in three stages to solve the problem.
Examples:
$array_for_step1 = array // Array for 1-st step
(
"ewery day" => "every day",
"to school" => "to univer",
);
$array_for_step2 = array // Array for 2-nd step
(
'I',
'go',
'metro',
);
$array_for_step3 = array // Array for last 3-rd step
(
"my" => "he",
"metro" => "bus",
);
Input text:
$input = "Ewery day I go To SchooL with metro. My friend too go to school but without metro.";
Comments to the decision stages:
Step 1:
Here you need to replace the keys of the array with their values in the text. Input text form after 1-st step replace using array $array_for_step1
:
Highlighted bold - changed words in the text.
Every day I go To UniveR with metro. My friend too go To UniveR but without metro.
Step 2:
Highlighted bold - words that do not need to be replaced in the next step
Here you need to find words from the array and mark them out from the text so that they are not replaced or not available for replacement for the third step. Input text form after 2-nd step replace using array `$array_for_step2`:
Every day I go To UniveR with metro. My friend too go To UniveR but without metro.
Step 3:
Here you need to replace the keys of the array with their values in the text. Input text form after 3-rd step replace using array $array_for_step3
:
Highlighted bold - Allocated those words that are not given a change.
Every day I go To UniveR with metro. My friend too go To UniveR but without metro.
Final result:
"Every day I go To UniveR with metro. My friend too go To UniveR but without metro."
My example function for replace array keys to value an any string cases:
function ReplKeyToValue($request, $dictionary) // $request = string && $dictionary associative array
{
$request = str_replace($search, $replace, $request); // replace special letters to default cyrillic letters
$result = preg_replace_callback("/\pL+/u", function ($m) use ($dictionary) {
$word = mb_strtolower($m[0]);
if (isset($dictionary[$word])) {
$repl = $dictionary[$word];
// Check for some common ways of upper/lower case
// 1. all lower case
if ($word === $m[0]) return $repl;
// 2. all upper case
if (mb_strtoupper($word) === $m[0]) return mb_strtoupper($repl);
// 3. Only first letters are upper case
if (mb_convert_case($word, MB_CASE_TITLE) === $m[0]) return mb_convert_case($repl, MB_CASE_TITLE);
// Otherwise: check each character whether it should be upper or lower case
for ($i = 0, $len = mb_strlen($word); $i < $len; ++$i) {
$mixed[] = mb_substr($word, $i, 1) === mb_substr($m[0], $i, 1)
? mb_substr($repl, $i, 1)
: mb_strtoupper(mb_substr($repl, $i, 1));
}
return implode("", $mixed);
}
return $m[0]; // Nothing changes
}, $request);
return $result;
}
I gave an example of the text and the value of the array in English. But this is so that you can decide and understand easily. But I need that your solution work for Cyrillic text and array.