S
S
Sergey Orlov2017-06-06 13:00:45
PHP
Sergey Orlov, 2017-06-06 13:00:45

How to modify the function to work correctly?

Good afternoon everyone! I have the following code to replace text:

$string = "raptor editor является редактором с открытым исходным кодом javascript wysiwyg html editor.";
 
$search = array("raptor editor","editor","javascript wysiwyg html editor","wysiwyg","html");
$replace = array("tinymce editor","false","jquery wysiwyg html editor","false","false");
 
$str = strtr($string,array_combine($search, $replace));
 
echo $str;

Answer: tinymce editor is an open source jquery wysiwyg html editor .
You need to make sure that this code replaces the text with the correct variant in any case . For example, if the $string variable is equal to:
$string = "RAPTOR editor является редактором с открытым исходным кодом javascript wysiwyg html editor.";
$search = array("raptor editor","editor","javascript wysiwyg html editor","wysiwyg","html");
$replace = array("tinymce editor","false","jquery wysiwyg html editor","false","false");
 
$str = strtr($string,array_combine($search, $replace));
 
echo $str;

Answer: RAPTOR false is an open source jquery wysiwyg html editor
The answer should be: TINYMCE editor is an open source jquery wysiwyg html editor.
And so on... In the array, all search and replace words will be in lowercase.
I have my own code which replaces all words in exact case. But it does not replace as it should, although it preserves the case. Here is the code:
function repl($string, $dict)
{
   $string = preg_replace_callback("/\pL+/u", function ($m) use ($dict) {
        $word = mb_strtolower($m[0]);
        if (isset($dict[$word])) {
            $repl = $dict[$word];
            if ($word === $m[0]) return $repl;
            if (mb_strtoupper($word) === $m[0]) return mb_strtoupper($repl);
            if (mb_convert_case($word,  MB_CASE_TITLE) === $m[0]) return mb_convert_case($repl,  MB_CASE_TITLE);
            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]; 
    }, $string);
 
   return $string;
}
 
$dict = array
 (
        "raptor editor" => "tinymce editor",
        "editor" => "false",
        "javascript wysiwyg html editor" => "jquery wysiwyg html editor",
        "wysiwyg" => "false",
        "html" => "false"
);
 
$string_1 = "Raptor Editor является редактором с открытым исходным кодом JavaScript WYSIWYG HTML editor.";
$string_2 = "raptor editor является редактором с открытым исходным кодом javascript wysiwyg html editor.";
$string_3 = "RaPTor EDitOr является редактором с открытым исходным кодом JAVAscript wYsIwYg HTML editor.";
 
// Тестирование скрипта:
 
echo repl($string_1, $dict); 
// Raptor False является редактором с открытым исходным кодом JavaScript FALSE FALSE false.
 
echo repl($string_2, $dict); 
// raptor false является редактором с открытым исходным кодом javascript false false false.
 
echo repl($string_3, $dict); 
// RaPTor FAlsE является редактором с открытым исходным кодом JAVAscript fAlSe FALSE false.

This script gets false , so you need to either make sure that there is a correct replacement without false or remake the first script case insensitive . And most importantly, I work with millions of data in an array, and here the speed of the script is important. The second script " repl() " is very fast but has problems. Maybe someone has some options. Thanks to everyone in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2017-06-06
@glebovgin

Okay, here's a case insensitive version of strtr

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question