165. PHP Program to find number of words which can occured more than once.
How does this program work?
- This program is used to find number of words repeated in given text using PHP.
Here is the code
<html>
<head>
<title>PHP Program To find number of words repeated in given text</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter a String"/> </td>
</tr>
<tr>
<td> <input type="ctext" name="num2" value="" placeholder="Enter search word"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
error_reporting(0);
if(isset($_POST['submit'])) {
$s = $_POST['num1'];
$w = $_POST['num2'];
$s1 = $s;
$w1 = $w;
$k = 0;
$found = 0;
$t = 0;
for($i=0;$s[$i];$i++)
{
if($s[$i]==' ')
{
$a[$k++] = $i;
}
}
$a[$k++] = $i;
$j = 0;
for($i=0;$i<$k;$i++)
{
$n = $a[$i]-$j;
//Comparing Words
if($n==strlen($w))
{
$t = 0;
for($l=0;$w[$l];$l++)
{
if($s[$l+$j]==$w[$l])
{
$t++;
}
}
//if word found then count increment
if($t==strlen($w))
{
$found++;
}
}
$j = $a[$i]+1;
}
echo "Given string: ".$s1."</br>";
echo "Search word: ".$w1."</br>";
echo ("The word $w occurred count= ".$found);
return 0;
}
?>
</body>
</html>