我尝试使用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
。
上面的列表更大,但这只是一个例子。我曾尝试重命名变种,但没有运气。
答案 0 :(得分: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);
答案 1 :(得分: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
映射到数组的键并将分隔符作为第二个参数传递。
答案 2 :(得分: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)];
答案 3 :(得分: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"
答案 4 :(得分: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;
}
}
答案 5 :(得分:-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;
}