Reputation: 1457
I'm trying to convert a two letter language abbreviations to full words using str_replace
.
The problem I'm having is that they're affecting each other when echoed out.
$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);
An example: when I echo out en
, I get EnglIcelandich
.
The list above is bigger but that is just a example. I have tried renaming the vars but with no luck.
Upvotes: 3
Views: 206
Reputation: 11
$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"
);
Using 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;
}
Upvotes: -1
Reputation: 101614
Unless you expect things like ensppt
as an original value, just use a mimic'd look-up table:
$lang_lookup = array(
'en' => 'English',
'es' => 'Spanish',
'pt' => 'Portuguese',
// ...
);
$lang = array_key_exists($lang, $lang_lookup) ? $lang_lookup[$lang] : 'Unknown';
Alternatively (but slower) you could copy one string to the other:
$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"
Upvotes: 0
Reputation: 1595
If $lang
will always be a two character string, there is no need for search/replace at all, just use a lookup table like this:
$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)];
Upvotes: 1
Reputation: 225074
You can do them all at once using preg_replace_callback
:
$map = array(
'en' => 'English',
...
);
$lang = preg_replace_callback('/' . implode('|', array_keys($map)) . '/', function($match) use ($map) {
return $map[$match[0]];
}, $lang);
Oh, and if any of your strings ever have any special characters in them, you'll need to map preg_quote
to your array's keys and pass the delimiter as its second argument.
Upvotes: 2
Reputation: 16573
It's simple! Use strtr
instead.
<?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);
Upvotes: 7
Reputation: 54801
If there is only support to be one occurrence in the string, then just check which one exists first. Using an array as a lookup table.
$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;
}
}
Upvotes: 0