PHP Diary | PHP Housekeeping | PHP Scripts | TD Scripts.com
TD Pic of Day PHP - Run your own picture of the day script from any site anywhere with this handy script


[back]
WB01624_.gif (281 bytes) 01/05/00 "Searching external or internal webpages" WB01626_.gif (272 bytes)[next]

Miss me yesterday? lol :)  I got a few emails saying, "I can't see the diary entry."  The reason you couldn't see one is because there wasn't one :) I am going to try and study PHP every day until I feel that I no longer need to study aloud and online. I work some days in excess of 15 hours and have no time to study, so if you see no diary entry, I either didn't learn anything new or I didn't study that particular day. Whenever I write something, I do it in a sense *live* therefore you shortly after I code the HTML and hit save, my FTP editor FTPs these changes immediately onto the website. I suppose I could just add an entry to say hello or something, or ramble aloud about what I ate for the day, but I think you would grade me down on the useful rating scale considerably if I did do that. Who knows, though? I may do that once in awhile.  Anyway, the readership of these diary entries is increasing, according to my access logs, and I thank you for tuning in, so to speak. How long these diary entries continue will be in direct proportion to the learning I do, and the number of email requests I get to explore PHP further. Please take a minute and use the form to send me ideas for future diary entries, if you would like to see me explore a new topic, or an existing one in greater depth. I have learned a great deal already and have started several new PHP scripts projects that will show up as full script downloads in the future. With the information in these diary entries, you should have been able to start some PHP projects of your own, as well :)

Searching website URLS

This diary is starting to grow with information and some type of search engine would be useful to allow people to get directly and quickly to the information they are interested in. In some ways a linear approach would be best (starting along with me at the beginning of the diary and moving through in chronological order, since this is an evolving website based on my learning in stages) and also since I have split my learning over several days. I wish I had more time to make a full lesson per day to keep this more cohesive, but such is the life of a true and yet (relatively) unedited diary. At any rate, if I wanted to find out about searching using PHP and wanted to get to it fast, a search engine in some capacity would be a nice feature for you (and me, in the future actually, when I need to reference back to something). Well that's what I'm working on next. PHP offers a pretty convenient way to search an entire page. How good is your memory? Let's write a quick script to search New Year's Day's diary entry for keywords. First, of course we need a search box:

Example #20: Simple search for keyword at a URL

<form method="POST" action="example20.php3">
<
p><input type="text" name="search_criteria" size="20">
<
input type="submit" value="Search!"></p
>
<
/form>

Nothing fancy needed for this beginning example. I have linked the box above to the script code I'm about to show you. Go ahead (no looking back and cheating now) and type a word or two you think was probably contained in that HTML page. All the script does is tell you whether or not it is there and how many times the search criteria appeared on each line of HTML. Here's the code to do this:

<html>
<body>
<?
$size = 0;
$url = "http://www.php-scripts.com/php_diary/010100.php3";
$the_page = fopen($url, "r");
while(!feof($the_page))
{
  $each_line = fgetss($the_page, 255);
    if(eregi($search_criteria, $each_line, $results))
   {
      // for each line where there is a match, increment a counter
      $size++;
   }
}
fclose($the_page);
print("I found $size ocurrences of $search_criteria at $url");
?>
</body>
</html>

The script above, although rudimentary, illustrates scanning an internal or external website URL. Another and maybe faster (I haven't done a benchmark test, so I am not sure) way of doing an internal (or local) search would be to simply open the file using the file command, fill an array and then check each item in the array. You just replace $url with the actual URL you want to search. Of special note is the function fgetss which is slightly different than fgets. With fgetss PHP attempts to remove any HTML or PHP code before matching. Again you could substitute eregi with ereg and thus have a case sensitive search. Therefore, it would be quite easy to create a branch of code that checked whether the search should be for exact matches or "similar" matches, the case being the identifier. Add a dropdown menu to your search form box with the name of "type" and then pass the value of "exact" for case sensitive matches. You would then modify the above code to read:

$each_line = fgetss($the_page, 255);
  if($type != "exact"
  {
      if(eregi($search_criteria, $each_line, $results))
     {
      // for each line where there is a match, increment a counter
      $size++;
     }
  }
   else
  {
       // ok let search using CASE SENSITIVE matches only
      if(ereg($search_criteria, $each_line, $results))
      {
         // for each line where there is a match, increment a counter
        $size++;
      }
  }

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

How useful was this diary entry? Avg Surfer Rating: 4.00 (1208)

[back]WB01624_.gif (281 bytes) 01/05/00 "Searching external or internal webpages" WB01626_.gif (272 bytes)[next]

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

Copyright 2000 php-scripts.com Last Modified 01/6/00 05:29