Why str_ireplace strange works?

Example:
$from = [ 'O','Ó','o','ó' ];
$to = [ "O,'Y','s','y' ];
$result = str_ireplace($from, $to, $text);

If $text contains:
O O
Ó ó

The result will be like this:
ABOUT
Have

I want to get the result:
About
Have

But I can't use a case-sensitive replacement, as AutoCorrect is, there are options comprised of many letters, and if the replacement is case sensitive, you have to make hundreds of choices in case one or more letters are great.
April 18th 20 at 12:54
3 answers
April 18th 20 at 12:56
Solution
The solution for the problem found here: How to make case insensitive replace?
April 18th 20 at 12:58
You will HAVE to use str_replace is case-sensitive.
Humble yourself and think of automatic generation of arrays $from and $to.
For example, I addition to the 'O', there are combinations like 'odelasi'. If I use case sensitive str_replace then I need to register all the variations like: Odelasi, oDelasi, oDelAsi. But such combinations as 'odelasi' I have under a thousand. Multiply the number of variations on the number of combinations and get a decision on an incredible number of rows. - orion_Erdm commented on April 18th 20 at 13:01
Is there option easier?

For example 'Ógera' and 'ógera' using str_ireplace will successfully be processed. No matter which register their letters in the middle. The output will always be: 'Uger' or 'Uger'. But with the usual Latin letters this does not come out. - orion_Erdm commented on April 18th 20 at 13:04
@orion_Erdm, there is an option, but you have to be an array
https://www.php.net/manual/ru/function.strtr.php - Lavern20 commented on April 18th 20 at 13:07
@Lavern20, this function is not case-sensitive? - orion_Erdm commented on April 18th 20 at 13:10
@orion_Erdm, where letters replace make, the register on their own options to register - Lavern20 commented on April 18th 20 at 13:13
@Lavern20, the incredible number of options? please read what I wrote above, we are talking about thousands of combinations. Even if it is a three-letter combinations, the number of rows will be too big. - orion_Erdm commented on April 18th 20 at 13:16
@orion_Erdm, and then the number of words or syllables, such letter => letter such that all occurrences will change - Lavern20 commented on April 18th 20 at 13:19
@Lavern20, despite the fact that these are the conditions of the problem.

Example:
dien = Thiey
die = te
di = ti
d = d
i = and
e = e
n = n

One case-insensitive combination of 4 letters already contains a bunch of options:
Dien DIen DIEn DIEN DiEn DiEN DieN, DIeN
dien dIen dIEn dIEN diEN diEn dieN dIeN, dIeN - orion_Erdm commented on April 18th 20 at 13:22
@orion_Erdm,
$trans = ['O' => 'On', 'Ó' => 'Y', 'o' => 'on', 'ó' => 'y',
'dien' => 'article',
'die' => 'te',
'di' => 'ti',
'd' => 'd',
'i' => 'and',
'e' => 'e',
'n' => 'n'
];

echo strtr('dien die di d i e n OÓoó', $trans); // tie te ti d I e n OO
- Lavern20 commented on April 18th 20 at 13:25
@Lavern20,

Such rules I had been prescribed. Now I want to solve a problem with the register.
To make it so:
dienier = tiene
Dienier = Tiene
dieNier = tiene
dienIer = tiene
dieNiEr = tiene
etc. - orion_Erdm commented on April 18th 20 at 13:28
Please note: str_ireplace() does not understand that most 'Ó' is an analog of the little 'ó'.
It is only a simple Latin 'D' and 'd' between associates.
That is why the results of TWO large 'D' and 'U' is not repeated, there is 'have'.
You will have to use registrationkey str_replace () because str_ireplace() his does not work. - Fleta_Hell commented on April 18th 20 at 13:31
@Fleta_Hell, Yes, I understand. The question how to make case-insensitive replacement (not source and target). - orion_Erdm commented on April 18th 20 at 13:34
Can use preg_replace(), in UTF-8 mode it completely case insensitive. - Fleta_Hell commented on April 18th 20 at 13:37
@Fleta_Hell, it also does not solve this problem. it is a case-insensitive replacement purposes. - orion_Erdm commented on April 18th 20 at 13:40
Well, it's your right to think that preg_replace () are case sensitive. Go to the men talk, and they do not know... - Fleta_Hell commented on April 18th 20 at 13:43
@Fleta_HellYou understand the difference between the source and the target?
preg_replace() with the flag /i is case insensitive to source replacement, but the purpose of You will not change.

Example:
'pier' => 'Pierre'
'PiEr' => 'Pierre' but not 'Pierre'. - orion_Erdm commented on April 18th 20 at 13:46
April 18th 20 at 13:00
If I understand correctly, it can help stunt double-replacement
First, case-insensitive, then without.

$result = str_replace($from, $to, $text);
$result = str_ireplace($from, $to, $result);


In General, judging by the description the author solves the problem of replacement of accented characters to counterparts without.
If so then try this script https://github.com/infralabs/DiacriticsRemovePHP

Another option is to use iconv() with the flag //TRANSLIT
$result = iconv('UTF-8', 'US-ASCII//TRANSLIT', $text);
Double replacement is impossible.
Here is a translation. Ie Latin characters in Russian. After the first operation, already Latin characters is not, then there will be nothing to replace it, so the result is two lines is = the result of the first row.

My goal: make AutoCorrect of the Latin character combinations into Russian with the preservation of register in the Russian language the same as it was in Latin. - orion_Erdm commented on April 18th 20 at 13:03

Find more questions by tags PHP