My sql save a number for each country. Now want to display country name from array.
Here problem is: If my country number is 11. str_replace
show '1' => 'Afghanistan',
twice. Not shown 11 no country '11' => 'Armenia',
My code:
function mycountry($text){
$toReplace = array(
'1' => 'Afghanistan',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia'
);
$replacement = array(
'1' => 'Afghanistan',
'7' => 'Anguilla',
'8' => 'Antarctica',
'9' => 'Antigua and Barbuda',
'10' => 'Argentina',
'11' => 'Armenia'
);
return str_replace($toReplace,$replacement,$text);
//return preg_replace('/$toReplace/','$replacement',$text);
}
// Sql query select user country
$country = 11;
echo ''.mycountry($country).'';
html display : AfghanistanAfghanistan
Remove the quotes in the arrays keys:
$toReplace = array(
1 => 'Afghanistan',
7 => 'Anguilla',
8 => 'Antarctica',
9 => 'Antigua and Barbuda',
10 => 'Argentina',
11 => 'Armenia'
);
and use any of the arrays straightaway as dictionary