Click here to Skip to main content
14,086,389 members
Rate this:
 
Please Sign up or sign in to vote.
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;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190518.1 | Last Updated 11 May 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100