-
Notifications
You must be signed in to change notification settings - Fork 1
/
01.PrintTags.php
38 lines (36 loc) · 1.58 KB
/
01.PrintTags.php
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
<!--Problem 1. Print Tags
Write a PHP script PrintTags.php which generates an HTML input text field and a submit button.
In the text field the user must enter different tags, separated by a comma and a space (", ").
When the information is submitted, the script should split the tags, put them in an array and print out the array.
Semantic HTML is required. Styling is not required.-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Problem 1 - Print Tags</title>
</head>
<body>
<section>
<p>Enter Tags:</p>
<form method="post" action="">
<input type="text" name="tags" id="tags">
<input type="submit" name="submitter" id="submitter" value="Submit">
</form>
<br />
</section>
</body>
</html>
<?php
if (isset($_POST['tags']) && strlen($_POST['tags']) > 0) { // Two checks for empty text field
$inputStr = htmlentities($_POST['tags']); // Getting the input tags as a string
$inputArr = explode(',', $inputStr); // Splitting the input string to separate array elements
$inputArr = array_map('trim', $inputArr); // Removing any whitespaces from the array elements
for ($i = 0; $i < count($inputArr); $i++) { // Iterating until the length of the array minus 1.
echo $i . ' : ' . $inputArr[$i] . '<br />'; // Printing the results
}
} elseif (strlen($_POST['tags']) == 0 && isset($_POST['submitter'])) { // If nothing has been entered in the text field
echo 'You have to submit something first!'; // but the submit button has been clicked, then...
} else {
die;
}
?>