Computing.Net > Forums > Web Development > find keyword and return next chrs

find keyword and return next chrs

Reply to Message Icon

Original Message
Name: vesper8
Date: January 25, 2006 at 10:49:26 Pacific
Subject: find keyword and return next chrs
OS: Windows XP-64
CPU/Ram: AMD 3700+ Diego / 1GB OCZ
Comment:

Hello again.

I'm building a script that will do the following things :

open a .htm file from a specified URL

search the .htm file for keywords

and return each instance of the keywords found AND the following 5 characters of each instance

for exemple.. I would specify that I'm looking for either "orange, apple or carrot" and it would return something like

orangePA.htm, orange17.gif, apple juic, carrot190.jp

you get the idea.. it would return each instance of the keywords i wanted and whatever 5 characters followed that word.. regardless of what those are

if possible..the findings would be written to a simple text file with each value seperated by commas

also.. the option to return the found keyword once and then ONLY the 5 following characters of each instance of that word found would be even better if not too tricky to code

thanks to anyone that solves this code challenge!


Report Offensive Message For Removal

Response Number 1
Name: Michael J (by mjdamato)
Date: January 25, 2006 at 11:15:30 Pacific
Subject: find keyword and return next chrs
Reply: (edit)

Language?

In the case of returning a keyword once and showing the mutiple instances of the 5 following characters how were you looking to have that returned?

Michael J


Report Offensive Follow Up For Removal

Response Number 2
Name: vesper8
Date: January 25, 2006 at 14:05:52 Pacific
Subject: find keyword and return next chrs
Reply: (edit)

language is php

the results would be written to a text file, on a single line, seperated by columns

i was thinking it could find the keyword, mark down the ending position of the keyword.. and print the next 5 characters.. then continue searching and doing this untill the keywords can't be found anymore. i know php has about 50 different string functions and don't know which to use to get this done


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: January 25, 2006 at 15:22:54 Pacific
Subject: find keyword and return next chrs
Reply: (edit)

I'll give you some suggestions and a little code examples, but you shuld at least attempt it. Then ask for help when you hit a wall. When trying to determine how to do anything in PHP I always go to PHP.net, it's the best source in my opinion.

If you look up the information for a string function, there will usually be links to related functions on the page. This is a good way to determine which is the best function to use in a particular case.

I will try to provide a means to get the keywords and their associated 5 charactrer values. I won't cover opening an htm file to read or writing out to a text file.

[NOTE: I have added periods at the beginning of some lines of code to aid in readability]

Assuming the contents of the htm file have been read into the variable $source:

First I would set up an array with the keywords to search for:

$keywords = array ('word1','word2','word3');

Then create control variables:

$keyword = "";
$foundSet = array();

Then loop through the keywords and do the heavy lifing:
//Loop through each keyword
foreach ($keywords as $value) {
.$position = 0;
.//Loop through all occurances of keyword
.while (strpos($source,$value,$position)) {
..//Set variable to start of 5 character value
..$foundPos = strpos($source,$value,$position)+strlen($keyword);
..//Create new record if 1st time found
..if ($keyword!=$value) {
...$keyword = $value;
...$foundSet[$keyword] = array();
..}
..$foundSet[$keyword][count($foundSet[$keyword])] = substr($source,$foundPos,5);
..//reset position
..$position = $foundPos+5;
.}
}

When all is said and done you should have an array in the format:

Array
(
..[keyword1] => Array (
....[0] => value1
....[1] => value2
....[2] => value3
....)
..[keyword2] => Array (
....[0] => value1
....[1] => value2
....[2] => value3
....)
..[keyword2] => Array (
....[0] => value1
....[1] => value2
....[2] => value3
....)
)

Michael J


Report Offensive Follow Up For Removal

Response Number 4
Name: vesper8
Date: January 25, 2006 at 20:11:32 Pacific
Subject: find keyword and return next chrs
Reply: (edit)

thank you so much for your help Michael.. after a bit of fiddling around I got the code to work almost perfectly.. I still have to find out how to 'echo' to a text file.. but for now i'm echoing onto a webpage and then copy/pasting that.

i couldn't of done it without your help. thanks a lot


Report Offensive Follow Up For Removal

Response Number 5
Name: Michael J (by mjdamato)
Date: January 26, 2006 at 07:52:18 Pacific
Subject: find keyword and return next chrs
Reply: (edit)

Glad I could help. Would you like my Paypal account? LOL.

Take a look at this page as a starting point on how to write to a file.

http://us2.php.net/manual/en/function.fwrite.php

Michael J


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: find keyword and return next chrs

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software