searching for a particular word in html files

I want to write a php code that searches for a particular word, provided by the user, in an html file. Basically I would like to craete a search engine that searches for number of occurances of a word in a current page and then notifies the user the places where it found the word.

Thank You
qader99Asked:
Who is Participating?
 
dutchartConnect With a Mentor Commented:
You have to tell apache for which files to start the PHP engine. Usually apache is configured to parse files with the extension .php3 with the engine. So try changing the extension .html to .php3 . If this doesn't work check your httpd.conf file in the apache conf directory. Be sure to have the following lines present in the file :

AddType application/x-httpd-php3 .php3

and

Action application/x-httpd-php3 /home/httpd/cgi-bin/php-4.0b3


If you also want to parse all .html files try this :

AddType application/x-httpd-php3 .html

However I wouldn't recommend this because also plain html files will be parsed for no good reason
0
 
gravityCommented:
<?
      $filename = "myfile.html";
      $contents = file($filename);
      $foundcount = 0;
      for ($i=0;$i<count($contents);$i++) {
            $tok = trim(strtolower(strip_tags(strtok($contents[$i]," "))));
            while ($tok) {
                  if ($tok==$searchword)
                        $foundcount++;
                  $tok = strtok(" ");
            }
      }
      
?>

OK then, no doubt EE has screwed up it's formatting, but anyway...

The script takes $searchword as the word that you are searching the file $filename for. The word count is stored in $foundcount and you can do what you want with it at the end of the script.
0
 
sdesarCommented:
interesting I have done the exactly this in Perl/CGI...
if you are interested let me know.
BTW...I am new to php...what is it?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gravityCommented:
A cooler version of Perl... you encode it in the HTML itself, but it is parsed on the server side so it only outputs HTML... if you tell it to ;)

Check out the official site : http://www.php.net
0
 
jasmoft146Commented:
However there is a problem with that, you don't include binary safe commands.  This could come into play if someone sends you the wrong type of file.   It is safer to do this:

<?php
$filename = "myfile.html";
$fd = fopen($filename, "r");
$count=0;
$done=0;
$seachFor="";
$buffer = fread( $fd , filesize($filename));
while ($done!=1){
$xt = (strpos($buffer, $searchFor));
if ($xt > "0"){
$buffer = substr($buffer, ($xt + strlen($searchFor)));
$count=$count+1
}
else {
done=1;
}
}

?>
0
 
sdesarCommented:
I am new to PHp and I want to test this code.
How do I place this code within the HTML document?

0
 
jasmoft146Commented:
<html>
<?php
.....
.....
.....
?>
</html>
0
 
sdesarCommented:
Does Php have to reside on a server or can I paste this on my school website that has netscape?
0
 
jasmoft146Commented:
PHP has to be on the server
0
 
sdesarCommented:
Okay I install a recent version
php 4.0 beta

Onto the Unix server at school, it resides in the same place as cgi-bin ..ie
/home/httpd/cgi-bin/php-4.0b3


and then I typed the following code
<html>
<head>
<title>Example</title>
</head>
<body>
<?php echo "Hi, I'm a PHP script!"; ?>
</body>
</html>


However, I am getting no results from this
indexphp.html document.  i can see that Example is displayed as the title.

Help Please!!
0
 
jasmoft146Commented:
Did you add the stuff to the appache config file?
0
 
sdesarCommented:
No, Where is that located?
0
 
sdesarCommented:
chanhed the file name to .php3 and the files code is displayed instead of -
Hi I am php script.

Is the script above  wrong?

Where is the httpd.conf file ?
0
 
sdesarCommented:
Opps ... I don't have the Apache_1.3.x
Module.
Where can I get that from...
www.php.net doesn't have it.
0
 
jasmoft146Commented:
Get apache at www.apache.org
0
All Courses

From novice to tech pro — start learning today.