I'm trying to make Country slug converter to short name. So I created this function:
<?php
function convertCountry( $countrySlug ) {
$countries = "Andorra: AR|United-Arab-Emirates: UAE|Afghanistan: AFG|Antigua-And-Barbuda: AAB|Anguilla: ANG|Albania: ALB|Armenia: ARM";
$countryArray = explode('|', $countries);
return array_search($countrySlug, $countryArray);
}
echo convertCountry('United-Arab-Emirates');
?>
It must be print UAE but doesn't work.
array_search
will find the equal text. In your case, I think we should use strpos
.
function strpos_array($countryArray, $countrySlug) {
if (is_array($countryArray)) {
foreach ($countryArray as $country) {
if (($pos= strpos($country, $countrySlug)) !== false) {
return str_replace($countrySlug.': ', '', $country);
};
}
}
return false;
}
function convertCountry($countrySlug) {
$countries = "Andorra: AR|United-Arab-Emirates: UAE|Afghanistan: AFG|Antigua-And-Barbuda: AAB|Anguilla: ANG|Albania: ALB|Armenia: ARM";
$countryArray = explode('|', $countries);
return strpos_array($countryArray, $countrySlug);
}
echo convertCountry('United-Arab-Emirates');
//This will print UAE