Hi,
I have a really simple problem with a test search application I'm trying to implement as a stop-gap until we launch our CMS.
This script basically finds files with a .html extenstion andereg's through the file to see if it contains $word.
I've been up nearly a whole day developing :o so I'm sure I'm missing the real simple solution but you know how it is...Could someone lend an eye and help me out?
Appolagies for he lengthy code... Brrrrrrrrr
TIA,
<?php
/*
Filename : searchapp_t.php
Description : Test Search App
This application gets a list of files from the `find' binary and
pushes each one into an array. If one of the words in the query
are found in that file a link to the file is printed to the
client.
*/
// Set-up the $search_dir to a location that this search app can
// look in for files ending in $file_ext.
$search_dir = '/home/exposu/sites/blingbling';
$file_ext = '*.html';
$find_bin = '/usr/bin/find';
// Run the `find' binary to scan through $search_dir and collect
// a list of files that end in $file_ext.
$find_out = `$find_bin $search_dir -name "*.html"`;
// Create an array of files that end in $file_ext. Also we need
// to create an array containing the the individual words from
// the query.
$files = explode ("\n", $find_out);
// Check that we have been given a search query:
if (!isset ($_GET["query"]))
{
// We haven't been iven a query :(
die ('$_GET["query"]: Not set!');
}
// Now create that array of query words.
$query_words = explode (" ", $_GET["query"]);
if (!is_array ($query_words))
{
die ('$query_words: Not an array!');
}
else
{
$query_w = array ();
foreach ($query_words as $qnum)
{
array_push ($query_w, $query_words[$qnum]);;
}
}
// Now for the fun part! We're going to walk through the array and
// see if we can match any of the words given in the query.
if (!is_array ($query_w))
{
die ('$query_w: Not an array!');
}
else
{
foreach ($files as $qnum => $s_file)
{
if (is_file ($s_file) && is_readable ($s_file))
{
$file_cont = implode ("\n", file ($s_file));
$file_links = array ();
foreach ($query_w as $qnum2)
{
$word = $query_w[$qnum2];
if (ereg ($word, $file_cont))
{
array_push ($file_links,$s_file);
}
}
}
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Search Results</TITLE>
<LINK REL="stylesheet" HREF="http://blingbling.exposure.org.uk/css.css"/>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<SPAN CLASS="bodyTitles">Search Results:</SPAN><BR/><BR/>
<P>Your search query yielded the following results:</P>
<?php
echo "\n";
if (!isset ($file_links) || !is_array ($file_links))
{
foreach ($file_links as $fnum)
{
echo " <A HREF=\"".$file_links[$fnum]."\">".$file_links[$fnum]."</A><BR/>\n";
}
}
else
{
echo "No matches found! Sorry :(\n";
}
//echo "\n<PRE>\n";
//echo $find_out;
//echo $file_cont;
//echo "</PRE>\n";
?>
</BODY>
</HTML>