PHP - 如何替换代码国家名称?

时间:2018-04-23 16:38:20

标签: php

我有那段代码:

<?php echo esc_html($resume->candidate_location) ?> <?php $string= esc_html($resume->candidate_country);
$search  = array("4", "248", "8", "12", "16", "20", "24", "660", "10", "28", "32", "51", "533", "36", "40", "31", "44", "48", "50", "52", "112", "56", "84", "204", "60", "64", "68", "535", "70", "72", "74", "76", "86", "96", "100", "854", "108", "116", "120", "124", "132", "136", "140", "148", "152", "156", "162", "166", "170", "174", "178", "180", "184", "188", "384", "191", "192", "531", "196", "203", "208", "262", "212", "214", "218", "818", "222", "226", "232", "233", "231", "238", "234", "242", "246", "250", "254", "258", "260", "266", "270", "268", "276", "288", "292", "300", "304", "308", "312", "316", "320", "324", "624", "328", "332", "334", "336", "340", "344", "348", "352", "356", "360", "364", "368", "372", "376", "380", "388", "392", "400", "398", "404", "296", "408", "410", "414", "417", "418", "428", "422", "426", "430", "434", "438", "440", "442", "446", "807", "450", "454", "458", "462", "466", "470", "584", "474", "478", "480", "175", "484", "583", "498", "492", "496", "499", "500", "504", "508", "104", "516", "520", "524", "528", "540", "554", "558", "562", "566", "570", "574", "580", "578", "512", "586", "585", "275", "591", "598", "600", "604", "608", "612", "616", "620", "630", "634", "638", "642", "643", "646", "654", "659", "662", "666", "670", "882", "674", "678", "682", "686", "688", "690", "694", "702", "534", "703", "705", "90", "706", "710", "239", "724", "144", "736", "740", "744", "748", "752", "756 ", "760", "158", "762", "834", "764", "626", "768", "772", "776", "780", "788", "792", "795", "796", "798", "800", "804", "784", "826", "840", "581", "858", "860", "548", "862", "704", "92", "850", "876", "732", "887", "894", "716");
$replace = array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe");
echo str_replace($search, $replace, $string); ?>

我想修改此代码以显示:

如果$ string是4 =阿富汗 如果$ string是248 =阿尔巴尼亚

因为现在当echo 756 时,我看到: “ 7Belize ”,但应该是:瑞士

我怎么能修复它以按代码显示完全国家?我确实试过找到一些解决方案,但我无法忍受它。下面是不同格式的国家/地区列表:

"4" => "Afghanistan"
"248" => "Åland Islands"
"8" => "Albania"
"12" => "Algeria"
"16" => "American Samoa"
"20" => "Andorra"
"24" => "Angola"
"660" => "Anguilla"
"10" => "Antarctica"
"28" => "Antigua and Barbuda"
"32" => "Argentina"
"51" => "Armenia"
"533" => "Aruba"
"36" => "Australia"
"40" => "Austria"
"31" => "Azerbaijan"
"44" => "Bahamas"
"48" => "Bahrain"
"50" => "Bangladesh"
"52" => "Barbados"
"112" => "Belarus"
"56" => "Belgium"
"84" => "Belize"
"204" => "Benin"
"60" => "Bermuda"
"64" => "Bhutan"
"68" => "Bolivia"
"535" => "Bonaire, Sint Eustatius and Saba"
"70" => "Bosnia and Herzegovina"
"72" => "Botswana"
"74" => "Bouvet Island"
"76" => "Brazil"
"86" => "British Indian Ocean Territory"
"96" => "Brunei Darussalam"
"100" => "Bulgaria"
"854" => "Burkina Faso"
"108" => "Burundi"
"116" => "Cambodia"
"120" => "Cameroon"
"124" => "Canada"
"132" => "Cape Verde"
"136" => "Cayman Islands"
"140" => "Central African Republic"
"148" => "Chad"
"152" => "Chile"
"156" => "China"
"162" => "Christmas Island"
"166" => "Cocos (Keeling) Islands"
"170" => "Colombia"
"174" => "Comoros"
"178" => "Congo"
"180" => "Congo, the Democratic Republic of the"
"184" => "Cook Islands"
"188" => "Costa Rica"
"384" => "Cote D'Ivoire"
"191" => "Croatia"
"192" => "Cuba"
"531" => "Curacao"
"196" => "Cyprus"
"203" => "Czech Republic"
"208" => "Denmark"
"262" => "Djibouti"
"212" => "Dominica"
"214" => "Dominican Republic"
"218" => "Ecuador"
"818" => "Egypt"
"222" => "El Salvador"
"226" => "Equatorial Guinea"
"232" => "Eritrea"
"233" => "Estonia"
"231" => "Ethiopia"
"238" => "Falkland Islands (Malvinas)"
"234" => "Faroe Islands"
"242" => "Fiji"
"246" => "Finland"
"250" => "France"
"254" => "French Guiana"
"258" => "French Polynesia"
"260" => "French Southern Territories"
"266" => "Gabon"
"270" => "Gambia"
"268" => "Georgia"
"276" => "Germany"
"288" => "Ghana"
"292" => "Gibraltar"
"300" => "Greece"
"304" => "Greenland"
"308" => "Grenada"
"312" => "Guadeloupe"
"316" => "Guam"
"320" => "Guatemala"
"324" => "Guinea"
"624" => "Guinea-Bissau"
"328" => "Guyana"
"332" => "Haiti"
"334" => "Heard Island and Mcdonald Islands"
"336" => "Holy See (Vatican City State)"
"340" => "Honduras"
"344" => "Hong Kong"
"348" => "Hungary"
"352" => "Iceland"
"356" => "India"
"360" => "Indonesia"
"364" => "Iran, Islamic Republic of"
"368" => "Iraq"
"372" => "Ireland"
"376" => "Israel"
"380" => "Italy"
"388" => "Jamaica"
"392" => "Japan"
"400" => "Jordan"
"398" => "Kazakhstan"
"404" => "Kenya"
"296" => "Kiribati"
"408" => "Korea, Democratic People's Republic of"
"410" => "Korea, Republic of"
"414" => "Kuwait"
"417" => "Kyrgyzstan"
"418" => "Lao People's Democratic Republic"
"428" => "Latvia"
"422" => "Lebanon"
"426" => "Lesotho"
"430" => "Liberia"
"434" => "Libyan Arab Jamahiriya"
"438" => "Liechtenstein"
"440" => "Lithuania"
"442" => "Luxembourg"
"446" => "Macao"
"807" => "Macedonia, the Former Yugoslav Republic of"
"450" => "Madagascar"
"454" => "Malawi"
"458" => "Malaysia"
"462" => "Maldives"
"466" => "Mali"
"470" => "Malta"
"584" => "Marshall Islands"
"474" => "Martinique"
"478" => "Mauritania"
"480" => "Mauritius"
"175" => "Mayotte"
"484" => "Mexico"
"583" => "Micronesia, Federated States of"
"498" => "Moldova, Republic of"
"492" => "Monaco"
"496" => "Mongolia"
"499" => "Montenegro"
"500" => "Montserrat"
"504" => "Morocco"
"508" => "Mozambique"
"104" => "Myanmar"
"516" => "Namibia"
"520" => "Nauru"
"524" => "Nepal"
"528" => "Netherlands"
"540" => "New Caledonia"
"554" => "New Zealand"
"558" => "Nicaragua"
"562" => "Niger"
"566" => "Nigeria"
"570" => "Niue"
"574" => "Norfolk Island"
"580" => "Northern Mariana Islands"
"578" => "Norway"
"512" => "Oman"
"586" => "Pakistan"
"585" => "Palau"
"275" => "Palestinian Territory, Occupied"
"591" => "Panama"
"598" => "Papua New Guinea"
"600" => "Paraguay"
"604" => "Peru"
"608" => "Philippines"
"612" => "Pitcairn"
"616" => "Poland"
"620" => "Portugal"
"630" => "Puerto Rico"
"634" => "Qatar"
"638" => "Reunion"
"642" => "Romania"
"643" => "Russian Federation"
"646" => "Rwanda"
"654" => "Saint Helena"
"659" => "Saint Kitts and Nevis"
"662" => "Saint Lucia"
"666" => "Saint Pierre and Miquelon"
"670" => "Saint Vincent and the Grenadines"
"882" => "Samoa"
"674" => "San Marino"
"678" => "Sao Tome and Principe"
"682" => "Saudi Arabia"
"686" => "Senegal"
"688" => "Serbia"
"690" => "Seychelles"
"694" => "Sierra Leone"
"702" => "Singapore"
"534" => "Sint Maarten"
"703" => "Slovakia"
"705" => "Slovenia"
"90" => "Solomon Islands"
"706" => "Somalia"
"710" => "South Africa"
"239" => "South Georgia and the South Sandwich Islands"
"724" => "Spain"
"144" => "Sri Lanka"
"736" => "Sudan"
"740" => "Suriname"
"744" => "Svalbard and Jan Mayen"
"748" => "Swaziland"
"752" => "Sweden"
"756" => "Switzerland"
"760" => "Syrian Arab Republic"
"158" => "Taiwan, Province of China"
"762" => "Tajikistan"
"834" => "Tanzania, United Republic of"
"764" => "Thailand"
"626" => "Timor-Leste"
"768" => "Togo"
"772" => "Tokelau"
"776" => "Tonga"
"780" => "Trinidad and Tobago"
"788" => "Tunisia"
"792" => "Turkey"
"795" => "Turkmenistan"
"796" => "Turks and Caicos Islands"
"798" => "Tuvalu"
"800" => "Uganda"
"804" => "Ukraine"
"784" => "United Arab Emirates"
"826" => "United Kingdom"
"840" => "United States"
"581" => "United States Minor Outlying Islands"
"858" => "Uruguay"
"860" => "Uzbekistan"
"548" => "Vanuatu"
"862" => "Venezuela"
"704" => "Viet Nam"
"92" => "Virgin Islands, British"
"850" => "Virgin Islands, U.s."
"876" => "Wallis and Futuna"
"732" => "Western Sahara"
"887" => "Yemen"
"894" => "Zambia"
"716" => "Zimbabwe"

有人可以帮助我吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您可以按长度对数组expected ndim=3, found ndim=2 进行排序,并相应地更改$search。因此,数字$replace将在数字756之前被替换。

基于你给定的关联数组:

56

输出:

  

瑞士

这是一个有效的demo

另一个例子:

$arr  = array(
  "56" => "Belgium",
  "756" => "Switzerland",
//...
);
krsort($arr);
$str = '756' ;
$out = str_replace(array_keys($arr), array_values($arr), $str);
echo $out;

输出:

  

瑞士 - 比利时