-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
52 lines (39 loc) · 1.25 KB
/
test.php
File metadata and controls
52 lines (39 loc) · 1.25 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h4>Enter a country from this list to find the capital:</h4>
<h5>> Australia, New Zealand, Canada, Portugal.</h5>
<form action="test.php" method="post">
<label for="">Enter a country: </label>
<input type="text" name="country">
<input type="submit"><br>
</form>
</body>
</html>
<?php
$capitals = array('Australia' => 'Canberra',
'New Zealand' => 'Wellington',
'Canada' => 'Ontario',
'Portugal' => 'Lisbon');
function format_each_word($str){
$words = explode(' ', $str);
$formattedWords = array();
for ($i = 0; $i < count($words); $i++) {
array_push($formattedWords, ucfirst(strtolower($words[$i])));
}
return implode(" ",$formattedWords);
}
$country_string = $_POST['country'];
$country = format_each_word($country_string);
$capital = $capitals[$country];
echo"<br>";
if ($capital) {
echo"The capital of {$country} is {$capital}";
} else echo"Sorry, \"{$country_string}\" is not on the list!";
?>