3

我正在尝试使用 . 将两个字母的语言缩写转换为完整的单词str_replace。我遇到的问题是它们在回显时会相互影响。

$lang = str_replace("en", "English", $lang); 
$lang = str_replace("es", "Spanish", $lang);
$lang = str_replace("pt", "Portuguese", $lang); 
$lang = str_replace("fr", "French", $lang);
$lang = str_replace("de", "German", $lang);
$lang = str_replace("it", "Italian", $lang);
$lang = str_replace("pl", "Polish", $lang);
$lang = str_replace("ru", "Russian", $lang);
$lang = str_replace("sv", "Spanish (El Salvador)", $lang);
$lang = str_replace("ko", "Korean", $lang);
$lang = str_replace("zh", "Chinese", $lang);
$lang = str_replace("nl", "Dutch", $lang);

一个例子:当我回显时en,我得到EnglIcelandich.

上面的列表更大,但这只是一个例子。我曾尝试重命名变量,但没有运气。

4

6 回答 6

7

这很简单!改为使用strtr

<?php
$replace = array(
    "en" => "English",
    "es" => "Spanish",
    "pt" => "Portuguese",
    "fr" => "French",
    "de" => "German",
    "it" => "Italian",
    "pl" => "Polish",
    "ru" => "Russian",
    "sv" => "Spanish (El Salvador)",
    "ko" => "Korean",
    "zh" => "Chinese",
    "nl" => "Dutch"
);

echo strtr("en it sv\n", $replace);
于 2013-01-15T01:32:30.493 回答
2

您可以使用以下命令一次完成所有操作preg_replace_callback

$map = array(
    'en' => 'English',
    ...
);

$lang = preg_replace_callback('/' . implode('|', array_keys($map)) . '/', function($match) use ($map) {
    return $map[$match[0]];
}, $lang);

哦,如果您的任何字符串中有任何特殊字符,您需要映射preg_quote到数组的键并将分隔符作为第二个参数传递。

于 2013-01-15T01:28:02.263 回答
1

如果$lang将始终是两个字符串,则根本不需要搜索/替换,只需使用如下查找表:

$languages = array(
    'en'=>'English' 
    'es'=>'Spanish'
    'pt'=>'Portuguese' 
    'fr'=>'French'
    'de'=>'German'
    'it'=>'Italian'
    'pl'=>'Polish'
    'ru'=>'Russian'
    'sv'=>'Spanish (El Salvador)'
    'ko'=>'Korean'
    'zh'=>'Chinese'
    'nl'=>'Dutch'   
);
$lang = $languages[strtolower($lang)];
于 2013-01-15T01:33:32.977 回答
0

除非您期望像ensppt原始值这样的东西,否则只需使用模仿的查找表:

$lang_lookup = array(
  'en' => 'English',
  'es' => 'Spanish',
  'pt' => 'Portuguese',
  // ...
);
$lang = array_key_exists($lang, $lang_lookup) ? $lang_lookup[$lang] : 'Unknown';

或者(但速度较慢)您可以将一个字符串复制到另一个:

$lang_lookup = array(
  'en'=>'English',
  'es'=>'Spanish',
  'pt'=>'Portuguese', 
  'fr'=>'French',
  'de'=>'German',
  'it'=>'Italian',
  'pl'=>'Polish',
  'ru'=>'Russian',
  'sv'=>'Spanish (El Salvador)',
  'ko'=>'Korean',
  'zh'=>'Chinese',
  'nl'=>'Dutch'   
);
$lang = 'es stands for Spanish';

$output = '';
for ($i = 0; $i < strlen($lang); $i++){
  $l = substr($lang, $i, 2);
  if (array_key_exists($l, $lang_lookup)){
    $output .= $lang_lookup[$l];
    $i++;
  } else {
    $output .= $lang[$i];
  }
}
echo $output; // "Spanish stands for Spanish"
于 2013-01-15T01:30:08.087 回答
0

如果只支持在字符串中出现一次,那么只需检查哪个首先存在。使用数组作为查找表。

$languages = array(
    'en'=>'English' 
    'es'=>'Spanish'
    'pt'=>'Portuguese' 
    'fr'=>'French'
    'de'=>'German'
    'it'=>'Italian'
    'pl'=>'Polish'
    'ru'=>'Russian'
    'sv'=>'Spanish (El Salvador)'
    'ko'=>'Korean'
    'zh'=>'Chinese'
    'nl'=>'Dutch'   
);

foreach($languages as $key=>$value)
{
    if(strpos($lang,$key) !== false)
    {
        $lang = str_replace($key,$value,$lang);
        break;
    }
}
于 2013-01-15T01:30:25.880 回答
-1
$entries = array(
    "en" => "English",
    "es" => "Spanish",
    "pt" => "Portuguese",
    "fr" => "French",
    "de" => "German",
    "it" => "Italian",
    "pl" => "Polish",
    "ru" => "Russian",
    "sv" => "Spanish (El Salvador)",
    "ko" => "Korean",
    "zh" => "Chinese",
    "nl" => "Dutch"
);

使用 str_replace:

for ( $i = 0 ;  $i < $entries['count']; $i++ ) {
    foreach($entries[$i] as $key => $entrie) {
        if ( is_string($key) ) {
        $result = str_replace('{'.$key.'}', $entrie[0], $result);
        }
    }

    $contact_data[] = $result;
}
于 2013-09-12T13:12:35.400 回答