本文介绍了我如何更正此PHP脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这个脚本是不敏感的,当我输入Pear来响应给出不正确的结果Pupil时。何它工作正确并将结果转换为utf-8?当我输入Саша 作为回应我给出结果???????而不是Михаил 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; 推荐答案 这篇关于我如何更正此PHP脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 04:05