Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
<!DOCTYPE html>
<html>
<head>
<title>Vowel Counter</title>
</head>
<body>
<h2>Enter a String</h2>
<form method="post">
<input type="text" name="inputString" required>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
function countVowels($str) {
$str = strtolower($str); // Make it case-insensitive
$vowels = ['a', 'e', 'i', 'o', 'u'];
$total = 0;
$occurrences = array_fill_keys($vowels, 0);
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
if (in_array($char, $vowels)) {
$occurrences[$char]++;
$total++;
}
}
return [$total, $occurrences];
}
$input = $_POST['inputString'];
list($totalVowels, $vowelCounts) = countVowels($input);
echo "<h3>Input String: " . htmlspecialchars($input) . "</h3>";
echo "<p>Total Number of Vowels: $totalVowels</p>";
echo "<p>Occurrences of Each Vowel:</p>";
echo "<ul>";
foreach ($vowelCounts as $vowel => $count) {
echo "<li>$vowel: $count</li>";
}
echo "</ul>";
}
?>
</body>
</html>