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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

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
dutchartCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.