PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
TD Forum - An unthreaded messageboard script to exchange ideas with your site visitors


[back]
WB01624_.gif (281 bytes) 01/08/00 "Searching through multiple files using arrays and loops" WB01626_.gif (272 bytes)[next]

Building a search array

Taking yesterday's code, I compiled a current list of diary entry files to use for searching through the meta tags. I simply reviewed  I didn't show you the code to save this compiled listing in a flat file called dat_files.txt. Instead of printing the filenames to the browser, I saved it to a file. Using the "w" will overwrite the existing contents of a file, which is what I want since I want to update the file information. I have already used the "a" to append files and the "r" to read files:

$update_file = fopen("/path/to/dat_files.txt", "w");
for($index = 0; $index < count($valid_filename); $index++)
{
    fputs($update_file, "$valid_filename[$index]\n");
}
fclose
($update_file);

Now whenever the script is called, it will update the $dat_files.txt with the most current diary entry listings and allow those who do a search using the code I'm going to write next, to be able to search the most current diary entry listings. I did not create an example for this code, but you can do it locally by replacing the last section of code yesterday with the code above and your proper path information. Remember that you do not need the path information if the $dat_files.txt file will go in the same directory as your script.

Loops and multiple file searches

I have used the while and for loops quite a bit already when going through arrays, but using them to go through multiple files is also a fairly common exercise. Using our newly created dat_files.txt file, I can now load an array using the file function and then loop through the array of filenames opening each file and trying to match the $search_criteria input by the enduser against the meta tags in the specified file. If there is a match, then we can add to the new $matched array, and then present the enduser with the search results and hyperlinks to navigate immediately to these diary pages.  I know the last few sentences are a mouthful but here is the code to do all the above:

<HTML>
<BODY>

<?
$url = "http://www.php-scripts.com/php_diary";
$the_page = file("/path/to/dat_files.txt");
sort($the_page);
for($index = 0; $index < count($the_page); $index++)
{
$the_page[$index] = ereg_replace("\n", "", $the_page[$index]);
$meta = get_meta_tags($the_page[$index]);
  if ($meta["description"] != "")
{
   if(eregi($search_criteria, $meta["description"]))
   {
     $results[] = $the_page[$index];
     $size++;
     $flag = 1;
   }
}
  if ($meta["keywords"] != "" && $flag < 1)
{
   if(eregi($search_criteria, $meta["keywords"]))
   {
     $results[] = $the_page[$index];
     $size++;
   }
}
  if ($flag == 1)
{
   $description[] = $meta["description"];
  $flag = 0;
}
}
$size = count($results);
print("I found $size occurences of $search_criteria in the diary pages meta tags");
if ($size > 0)
{
print("<br>Here are your urls to visit:<p><ol>");
  for($index = 0; $index < $size; $index++)
{
   print("<li>$description[$index] ");
   print("<a href=\"$url/$results[$index]\">");
   print("$results[$index]</a></li>");
}
print("</ol>");
}
?>

</body>
</html>


Example #23: Searching meta tags in multiple files or URLs (I've now added this search box to the main diary page)

Notes: I use the $flag to determine if a match for the meta title on the page was already made so we can ignore trying to match the meta keywords too and thus end up with duplicate urls displayed. If you wanted to add a branch of code so you could check for case sensitive matches you easily could. I showed how to print the $results array containing the links to pages and their appropriate meta descriptions in a listed order.

It would be easy to make your $dat_files.txt contain full URLs so you could search through the meta tags of internal or external URLs too. PHP makes it very convenient to search files and/or URLs. Keep in mind that if a page you specify does not contain any meta description or meta keyword tags, including the file is somewhat pointless (but the code above has a trap for that -- see the != "" which means go ahead and look for a match if you find something). If you do that, eliminate the $url variable from the code above, and make a text file with hyperlinks separated by space like this:

http://www.anydomain.com1/
http://www.anydomain.com2/any_html_page.html
http://www.anydomain.com2/any_shtml_page.shtml
http://www.anydomain.com3/any_php_page.php3

You could also create a form to enter multiple links into a text file at one time to do this. That might be a good exercise.

Please vote on what you think of this diary entry :)

How useful was this diary entry? Avg Surfer Rating: 4.01 (67)

[back]WB01624_.gif (281 bytes) 01/08/00 "Searching through multiple files using arrays and loops" WB01626_.gif (272 bytes)[next]

PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com

Copyright 2000 php-scripts.com Last Modified 01/8/00 04:54