<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$text = $_POST["text"];
$counter = array();
$upperText = strtoupper($text);
// Count instances of each character
for ($i = 0; $i < strlen($upperText); $i++) {
$ch = $upperText[$i];
if (!isset($counter[$ch])) {
$counter[$ch] = 0;
}
$counter[$ch]++;
}
// Sort the counter array alphabetically and numerically
ksort($counter);
// Display the result
echo "<h2>Character Count</h2>";
echo "<table>";
echo "<tr><th>Character</th><th>Count</th></tr>";
foreach ($counter as $char => $count) {
echo "<tr><td>$char</td><td>$count</td></tr>";
}
echo "</table>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Character Instance Counter</title>
<style>
body {
font-family: Arial, sans-serif;
}
form {
margin-bottom: 20px;
}
input[type="submit"] {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type="submit"]:hover {
background-color: #45a049;
}
table {
border-collapse: collapse;
width: 50%;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<h1>Character Instance Counter</h1>
<form method="post" action="">
<label for="text">Enter Text:</label><br>
<textarea id="text" name="text" rows="4" cols="50"></textarea><br>
<input type="submit" value="Count Instances">
</form>
</body>
</html>