Hey, I want to know if there is an easier way to do this then the way I have now. I've been searching for awhile but can't find something that exactly fits my needs, so I made this for now but I would like to know if there's a simpler way to do it.
I want to read a list of words or sentences from a text file, then store them in an array. Then I want to be able to run a search and find all lines of the array that have the searched word in it, then store the whole line into a new array.
All I have been finding on the net is ways to search an array for a word, then tell me if it's there or not, which isn't what I want it to do.
Yeah, I'm only new to php, so I most likely missed a simpler solution.
This is what I have made up.
<html>
<body>
<?php
$array = "array"; //The name of the text file that contains the list of words
$w = 0;
$file = @fopen($array . ".txt", "r") or die("Could not open file, contact web master");
while (!feof($file))
{
$arraySearch[$w] = fgets($file,4096);
$w+=1;
}
echo "This is what the first array contains.<p>";
$a = 0;
while ($a < $w)
{
echo $arraySearch[$a] . "<br>";
$a++;
}
if (isset($_GET['search']))
{
$search = $_GET['search'];
$a = 0;
$s = 0;
while ($a < $w & $search != "")
{
$find = explode("$search", $arraySearch[$a]);
$num = count($find);
$num--;
$l = 1;
if ($num != 0)
{
$id = $find[0];
while ($l <= $num)
{
$id = $id . $search . $find[$l];
$l++;
}
$arrayResult[$s] = $id;
$s++;
}
$a++;
}
$a = 0;
echo "<p>Your search returned the following.<p>";
while ($a < $s)
{
echo $arrayResult[$a] . "<br>";
$a++;
}
if ($a == 0)
{
echo "No results.";
}
}
?>
<form method="get">
<input type="text" name="search">
<input type="submit" value="Search">
</form>
</body>
</html>