This script is "insensitive" and when I enter "Pear" to response a give uncorrect result "Pupil". Ho do it work correct and convert result to utf-8? When I enter "Саша"
in response i give result "???????" and not "Михаил"
$data = 'Олег Саша Тимур they are students';
$search = array("they are","Олег","Саша","pear");
$replace = array("the're","Коля","Михаил","pupil");
$arr = array_combine($search, $replace);
$pat = '/\b(?:' . implode("|", array_keys($arr)) . ')\b/i';
$data = preg_replace_callback($pat, function($matches) use ($arr)
{ $i=0;
return !empty($arr[strtolower($matches[0])]) ?join('',array_map(function($char) use ($matches, &$i)
{
return ctype_lower($matches[0][$i++])?strtolower($char):strtoupper($char);
}, str_split($arr[strtolower($matches[0])]))) : $matches[0];
}, $q);
echo $data;
What I have tried:
$data = 'Олег Саша Тимур they are students';
$data = mb_convert_encoding($data, "UTF-8");
$search = array("they are","Олег","Саша","pear");
$replace = array("the're","Коля","Михаил","pupil");
$search = array_map("utf8_encode", $search);
$replace = array_map("utf8_encode", $replace);
$arr = array_combine($search, $replace);
$arr = array_map("utf8_encode", $arr);
$pat = '/\b(?:' . implode("|", array_keys($arr)) . ')\b/i';
$data = preg_replace_callback($pat, function($matches) use ($arr)
{ $i=0;
return !empty($arr[mb_strtolower($matches[0])]) ?join('',array_map(function($char) use ($matches, &$i)
{
return ctype_lower($matches[0][$i++])?mb_strtolower($char):mb_strtoupper($char);
}, str_split($arr[mb_strtolower($matches[0])]))) : $matches[0];
}, $q);
$data = mb_convert_encoding($hint, "UTF-8");
echo $data;