Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search, Count using PHP

Status
Not open for further replies.

mayamanako

Technical User
Aug 31, 2005
113
GB
hi guys, i'm a newbie in php and would like to ask for some guidance.

i have a .txt file with the list of words something like this:

apple
banana
orange
grapes
.
.
.

and have also another body of text(probably more than a 1000 words) which i'd like to search and count the number of instances of the words like apple, banana, etc that it contains and list them, as an example, in this manner:

apple 5
banana 3
orange 1
.
.
.

is there a way to do it in php without using sql database, just pure php?

thanks for any help.

 
Code:
<?php
$firstFile = '/path/to/file/name/txt';
$secondFile = '/path/to/second/file/txt';

//get the contents of the files
$needles = file_get_contents($firstFile);
$haystack = file_get_contents($secondFile);

//convert the strings to arrays, breaking at new lines
$needles = explode("\n", $needles);
$haystack = explode("\n\, $needles);

//run the needles through trim to make sure there are no trailing spaces
$needles = array_map("trim"), $needles);

//instantiate a results holder
//this is optional as php will do this automatically but is still good practice.
$results = array();

//iterate the needles array
foreach($needles as $needle):

  //ensure that this is not a blank line.  if it is, then skip
  if (strlen($needle) == 0 || empty($needle)) continue;

  //search the haystack for the needle and return an array of lines where (just) it is found
  $r = array_keys($haystack, $needle);
  $results[$needle] = array('foundAtLines' => $r, 'count'=>count($r));

endforeach;

echo '<pre>';
print_r($results);
?>
NB code not tested but even if there are syntax errors, it should give you a good idea of a reasonable approach. this approach is processor friendly but not memory friendly. If you are running in very limited memory then you will want to use step-by-step i/o functions like fread and direct comparisons rather than the (optimized) array searching built-in functions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top