Convert replace Country Names with Country Codes php

Here is the code for how to convert / replace country names with country codes, it is not a function, I’m just using basic str_replace php syntax.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
$string = str_replace("United States", "US", $string);
$string = str_replace("Canada", "CA", $string);
$string = str_replace("New Zealand", "NZ", $string);
$string = str_replace("United Kingdom", "GB", $string);
$string = str_replace("Pakistan", "PK", $string);
$string = str_replace("Australia", "AU", $string);
$string = str_replace("Venezuela", "VE", $string);
$string = str_replace("Tanzania, United Republic of", "TZ", $string);
$string = str_replace("Hungary", "HU", $string);
$string = str_replace("India", "IN", $string);
$string = str_replace("Philippines", "PH", $string);
$string = str_replace("Nigeria", "NG", $string);
$string = str_replace("Bangladesh", "BD", $string);
$string = str_replace("Colombia", "CO", $string);
$string = str_replace("Mexico", "MX", $string);
$string = str_replace("Indonesia", "ID", $string);
$string = str_replace("Thailand", "TH", $string);
$string = str_replace("Argentina", "AR", $string);
$string = str_replace("Kuwait", "KW", $string);
$string = str_replace("Botswana", "BW", $string);
$string = str_replace("Oman", "OM", $string);
$string = str_replace("Malaysia", "MY", $string);
$string = str_replace("Ghana", "GH", $string);
$string = str_replace("Papua New Guinea", "PG", $string);
$string = str_replace("Saudi Arabia", "SA", $string);
$string = str_replace("South Africa", "ZA", $string);
$string = str_replace("Morocco", "MA", $string);
$string = str_replace("Egypt", "EG", $string);
$string = str_replace("Ecuador", "EC", $string);
$string = str_replace("Italy", "IT", $string);
$string = str_replace("Croatia", "HR", $string);
$string = str_replace("Zambia", "ZM", $string);
$string = str_replace("Portugal", "PT", $string);
$string = str_replace("Trinidad and Tobago", "TT", $string);
$string = str_replace("Germany", "DE", $string);
$string = str_replace("El Salvador", "SV", $string);
$string = str_replace("Algeria", "DZ", $string);
$string = str_replace("Mongolia", "MN", $string);
$string = str_replace("Lesotho", "LS", $string);
$string = str_replace("Sweden", "SW", $string);
$string = str_replace("Costa Rica", "CR", $string);
$string = str_replace("Qatar", "QA", $string);
$string = str_replace("Kenya", "KE", $string);
$string = str_replace("Panama", "PA", $string);
$string = str_replace("Jamaica", "KM", $string);
$string = str_replace("Brazil", "BR", $string);
$string = str_replace("Bahamas", "BS", $string);
$string = str_replace("Russia", "RU", $string);
$string = str_replace("Greece", "GR", $string);
$string = str_replace("Norway", "NO", $string);
$string = str_replace("Ethiopia", "ET", $string);
$string = str_replace("Israel", "IL", $string);
$string = str_replace("Hashemite Kingdom of Jordan", "JO", $string);
$string = str_replace("Lesotho", "LS", $string);
$string = str_replace("Serbia", "RS", $string);
$string = str_replace("Ukraine", "UA", $string);
$string = str_replace("Denmark", "DK", $string);
$string = str_replace("Romania", "RO", $string);
$string = str_replace("Guyana", "GY", $string);
$string = str_replace("Paraguay", "PY", $string);
$string = str_replace("Netherlands", "NL", $string);
$string = str_replace("Puerto Rico", "PR", $string);
$string = str_replace("Singapore", "SG", $string);
$string = str_replace("Guatemala", "GT", $string);
$string = str_replace("Uganda", "UG", $string);
$string = str_replace("Dominican Republic", "DO", $string);
$string = str_replace("Ivory Coast", "CI", $string);
$string = str_replace("American Samoa", "AS", $string);
$string = str_replace("Vietnam", "VN", $string);
$string = str_replace("Uruguay", "UY", $string);
$string = str_replace("Honduras", "HN", $string);
$string = str_replace("Iran", "IR", $string);
$string = str_replace("Spain", "ES", $string);
$string = str_replace("Mauritius", "MU", $string);
$string = str_replace("Liberia", "LR", $string);
$string = str_replace("Finland", "FI", $string);
$string = str_replace("Czechia", "CZ", $string);
$string = str_replace("Poland", "PL", $string);
$string = str_replace("Fiji", "FJ", $string);
$string = str_replace("Malta", "MT", $string);
$string = str_replace("Ireland", "IE", $string);
$string = str_replace("United Arab Emirates", "AE", $string);
$string = str_replace("Peru", "PE", $string);
$string = str_replace("Japan", "JP", $string);
$string = str_replace("Guam", "GU", $string);
$string = str_replace("Slovak Republic", "SK", $string);
$string = str_replace("Lebanon", "LB", $string);
$string = str_replace("Tunisia", "TN", $string);
$string = str_replace("Peru", "PE", $string);
$string = str_replace("Somalia", "SO", $string);
$string = str_replace("Cambodia", "KH", $string);
$string = str_replace("Belarus", "BY", $string);
$string = str_replace("Namibia", "NA", $string);
$string = str_replace("U.S. Virgin Islands", "VI", $string);
$string = str_replace("Chile", "CL", $string);
$string = str_replace("Suriname", "SR", $string);
$string = str_replace("Belgium", "BE", $string);
$string = str_replace("Senegal", "SN", $string);
$string = str_replace("Rwanda", "RW", $string);
$string = str_replace("Benin", "BJ", $string);
$string = str_replace("Zimbabwe", "ZW", $string);
$string = str_replace("Mauritania", "MR", $string);
$string = str_replace("Burundi", "BI", $string);
$string = str_replace("Turkey", "TR", $string);
$string = str_replace("Hong Kong", "HK", $string);
$string = str_replace("Slovenia", "SI", $string);
$string = str_replace("Djibouti", "DJ", $string);
?>

$string contains the list with the countries.
You will basicaly get another list with the country codes intead.

ADD A COMMENT: