<?php
//function for letter count return an assoc array
function letter_count($string){
$length = strlen($string);
$words = array();
for($i = 0; $i < $length; $i++){
if($string[$i] == " "){
continue;
}
if(array_key_exists($string[$i], $words)){
$words[$string[$i]] ++;
}else{
$words[$string[$i]] = 1;
}
}
return $words;
}
if(isset($_POST['string'])){
$string = $_POST['string'];
$words = letter_count($string);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>letter count</title>
<link rel="stylesheet" href="css/bootstrap.css">
<style>
.green{
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label for="string">give a sentence</label>
<input type="text" name="string" class="form-control" id="string" placeholder="Enter string">
</div>
<button type="submit" name="" class="btn btn-default">Submit</button>
</form>
<?php if(isset($words)) : ?>
<table class="table table-striped">
<thead>
<tr>
<th>words</th>
<th>frequency</th>
</tr>
</thead>
<tbody>
<?php
foreach($words as $key => $value) :
?>
<tr>
<td><?php echo $key; ?></td>
<td class="<?php if($value > 5){echo 'green';} ?>"><?php echo $value; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif ;?>
</div>
</body>
</html>
//function for letter count return an assoc array
function letter_count($string){
$length = strlen($string);
$words = array();
for($i = 0; $i < $length; $i++){
if($string[$i] == " "){
continue;
}
if(array_key_exists($string[$i], $words)){
$words[$string[$i]] ++;
}else{
$words[$string[$i]] = 1;
}
}
return $words;
}
if(isset($_POST['string'])){
$string = $_POST['string'];
$words = letter_count($string);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>letter count</title>
<link rel="stylesheet" href="css/bootstrap.css">
<style>
.green{
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label for="string">give a sentence</label>
<input type="text" name="string" class="form-control" id="string" placeholder="Enter string">
</div>
<button type="submit" name="" class="btn btn-default">Submit</button>
</form>
<?php if(isset($words)) : ?>
<table class="table table-striped">
<thead>
<tr>
<th>words</th>
<th>frequency</th>
</tr>
</thead>
<tbody>
<?php
foreach($words as $key => $value) :
?>
<tr>
<td><?php echo $key; ?></td>
<td class="<?php if($value > 5){echo 'green';} ?>"><?php echo $value; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif ;?>
</div>
</body>
</html>