# Thread: Searching for code, matching it, then echoing results.

1. ## Searching for code, matching it, then echoing results.

I don't even know where to begin with this on my own, so I'm looking to YOU for some guidance.

Basically I have a txt file that has a few words in it, one word per line. I need a script that will look through that txt file, find the words and then see if there are any other matches for that word in the file. Once that has completed I want it to show what words it searched for and how many times those words were found.

I tried using preg_match_all, but I can't figure out how to get the results I need. Can anyone help?

2. How big are these files? It may be better to read the file into an associative array like
$array[$word] = $count; Then you can easily get all of the words (array_keys) and the count$array[$word]; 3. The files are not going to be very big at all, would you still recommend doing that? 4. Yep. I like arrays a lot. My only concern was if the file gets too big, you're holding a lot in memory. 5. So, pardon my noobness, but how do I go about this? I still can't figure out how to read the file, find a word, find the matches, then say how many matches were found. My main problem is that the words aren't predefined, the script has to find them on it's own. Is this even possible? 6. Here's some modified code I use for something similar: PHP Code: $export = "\path\to\file\customer.txt";if (!is_readable($export)) { echo('Error connecting to the file ("' .$export . '").');    exit();}$store_array = array();$handle = @fopen($export, "r");while (!feof($handle)) {    $buffer = fgets($handle, 4096);    $word = ereg_replace("(\n|\r)", "",$buffer);    //removes partial line breaks    if (!isset($store_array[$word])) $store_array[$word] = 1;    else $store_array[$word]++;}fclose($handle);  I didn't test this code, but it's pretty similar to what I use. Now you can see if a word was found using (isset($store_array[$your_word])) and even see how many times it was found ($store_array[$your_word]). P.S. The code's a little dirty, but maybe another user will clean it up for you 7. PHP Code:  function array_repeated($array ) {     if ( ! is_array($array) ) { return false; }$duplicates = array();     foreach ( $array as$key => $val ) { // Set the internal pointer of an array to its last element end($array );         // Fetch a key from an associative array         $k = key($array );         // Return the current element in an array          $v = current($array );         while ( $k !==$key )         {             if ( $v ===$val )             {                 $duplicates[$key] = $v; } // Rewind the internal array pointer$v = prev( $array );$k = key( $array ); } } return$duplicates; } //  Load  file  into  Array $list = file( 'x.txt' ); // Convert every line to lowercase$list = array_map( "strtolower", $list ); print_r( array_repeated($list ) ); echo count( array_repeated( $list ) );  8. So you're just looking for values which were repeated? PHP Code:  <?php function moreThanOne($val){     return ($val > 1) ? true : false; }$file = file_get_contents('yourfile.txt'); $words = explode("\n",$file); $repeats = array_count_values($words); $repeats = array_filter($repeats, 'moreThanOne'); var_dump($repeats);  Or, if you like it consise: PHP Code:  <?php function moreThanOne($val){     return ($val > 1) ? true : false; }$repeats = array_filter(array_count_values(explode("\n", file_get_contents('yourfile.txt'))), 'moreThanOne'); var_dump(\$repeats); 

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•