158. PHP Program to find the occurences of each character of given string.
How does this program work?
- This program is used to read a string and find the occurences of each character using PHP.
- By using recursion we can find the reverse of given numbers.
Here is the code
<html>
<head>
<title>PHP Program To find the occurences of each character of given string</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="string" value="" placeholder="Enter a string"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$input = $_POST[ 'string'];
$str = $input;
echo "Given string : ".$str."<br>";
$input = str_replace(' ', '', $input);
$characters = str_split($input);
$outputs = [];
foreach ($characters as $char)
{
if(!isset($outputs[$char])) {
$outputs[$char] = 1;
}
else
{
$outputs[$char] += 1;
}
}
echo "Count of character in string :"."<br>" ;
foreach ($outputs as $char => $number)
{
echo $char . ' appeared ' . $number . ' times;<br>';
}
}
?>
</body>
</html>