There is a pattern. It has entries of the type {_LANG_}{ru}сутки{en}day{_LANG_} . There is a $slln variable that contains the selected language. My asked to replace the entire record with the text of the selected language. For example, if $slln='en' , then the entire entry should be replaced with day . I understand that one team does not do this, so I do this:

preg_match_all("/{_LANG_}(.*){_LANG_}/",$overall_output,$mt);

Here the result is as follows:

 $mt = Array ( [0] => Array ( [0] => {_LANG_}{ru}сутки{en}day{_LANG_} [1] => {_LANG_}{ru}неделю{en}week{_LANG_} [2] => {_LANG_}{ru}месяц{en}month{_LANG_} [3] => {_LANG_}{ru}все время{en}all time{_LANG_} ) [1] => Array ( [0] => {ru}сутки{en}day [1] => {ru}неделю{en}week [2] => {ru}месяц{en}month [3] => {ru}все время{en}all time ) ) 

The question is how to remove all values ​​from $mt[1] order to get something like:

 Array ( [0] => Array ( [ru] => сутки [en] => day ) [1] => Array ( [ru] => неделю [en] => week ) ... ) 

    2 answers 2

    Thanks, but I figured it out to be even better. Considering that I already know the language in advance and the text can also contain curly braces and hyphenation, too, the following happened:

     $rl='en'; preg_match_all("/{_LANG_}.*?{".$rl."}(.*?)(?:{[a-zA-Z]+}.*?|){_LANG_}/s",$overall_output,$mt); 

    Result:

     $mt = Array ( [0] => Array ( [0] => {_LANG_}{en}day {скобки}{ru}сутки{_LANG_} [1] => {_LANG_}{ru}неделю{en}week{_LANG_} [2] => {_LANG_}{ru}месяц{en}month{_LANG_} [3] => {_LANG_}{ru}все время{en}all time{_LANG_} ) [1] => Array ( [0] => day {скобки} [1] => week [2] => month [3] => all time ) ) 

      If we agree that the text does not contain curly brackets, then you can use the following regular expression :

       /(?<=({ru}|{en}))([^{]+)/g 

      For example:

       function tr($text, $lang) { if (preg_match_all('/(?<=({[\w]{2}}))([^{]+)/', $text, $matches)) { $lang = '{' . strtolower($lang) . '}'; foreach ($matches[1] as $key => $_lang) { if (strtolower($_lang) == $lang) { return $matches[2][$key]; } } } return ''; } echo tr('{ru}сутки{en}day', 'ru') . "\n"; // сутки echo tr('{ru}сутки{en}day', 'en') . "\n"; // day echo tr('{ru}сутки{en}day', 'fr') . "\n"; // 

      If the result is needed exactly in the form that you specified, then:

       function trArr(array $texts) { $translates = []; foreach ($texts as $key => $text) { if (preg_match_all('/(?<=({[\w]{2}}))([^{]+)/', $text, $matches)) { foreach ($matches[1] as $key2 => $lang) { $translates[$key][trim($lang, '{}')] = $matches[2][$key2]; } } } return $translates; }