3v4l.org

run code in 300+ PHP versions simultaneously
<?php $Ucountries = ["Uganda", "Ukraine", "United Arab Emirates (UAE)", "United Kingdom (UK)", "United States of America (USA)", "Uruguay", "Uzbekistan"]; $Mcountries = ["Macedonia (FYROM)", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Montenegro", "Morocco", "Mozambique", "Myanmar (Burma)"]; $str = "countries_mexico_mexico_city"; //$str = "countries_united_states_ohio"; $str = str_replace("countries_", "", $str); // Copy array needed to $CountryList $CountryList = ${strtoupper(substr($str,0,1)). "countries"}; $countryarr = array(); $arr = explode("_", $str); Foreach($arr as $val){ Foreach($CountryList as $item){ If(stripos($item, $val) !== false){ If(!in_array($val, $countryarr)){ $countryarr[] = $val; } } } } $country = implode(" ", $countryarr); $state_city = preg_replace("/" . implode("_",$countryarr) . "/", "", $str,1); $state_city = trim(str_replace("_", " ", $state_city)); Echo $country . "\n" . $state_city;
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
mexico mexico city

preferences:
147.89 ms | 404 KiB | 293 Q