Searching a small textfile

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
killervee3
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 6:53 am

Searching a small textfile

Post by killervee3 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hey all, I wondered if anyone colud help me, I need a basic search function.  
 To search a list of terms with there explainations from a .txt file and output snippets of the content ( 5 terms at a time).

An example of the .txt file would be:--

#------apple
A great fruit with many varieties.

#------apple/GrannySmith
A type of Apple

#------Orange
Another fruit with looks like exactly what it says on the tin.


etc... (with many more terms)

and my code for the search so far is:-

Code: Select all

<?php
//
// Class for finding a key in a multi-dimensional array
// This is specific to my needs, not general
//
class key_search
{
  var $found = array();

  // Constructor method
  function key_search($search, $array = '', $case = 0)
  {
    // Set up the parameters
    $search = (!is_array($search)) ? array($search) : $search;
    $array = (!is_array($array)) ? array($array) : $array;
    $ereg = ($case) ? 'ereg' : 'eregi';

    //
    // Search through the array keys for a match
    //

    // Loop through keys
    foreach (array_keys($array) as $key)
    {
      // Loop through search targets
      foreach ($search as $target)
      {
        // Return false if an empty string
        if ($target == '')
        {
          return false;
        }

        // If target expression matches the key and the resolved key in array exists
        if ($ereg($target, $key) && $array[$key][0])
        {
          // Put the resolved key in the list
          $this->found[] = $array[$key][0];
        }
      }
    }

    // Loop through values
    foreach ($array as $value)
    {
      // If the value is an array, search in it
      if (is_array($value))
      {
        $this->key_search($search, $value, $case);
      }
    }
  }
}

//
// Class for getting an array of tags from a file
//
class file_tags
{
  var $list = array();
  var $blocks = array();

  // Constructor method
  function file_tags($file, $marker = '', $separator = '/')
  {
    // Get the raw contents of the file
    $contents = file_get_contents($file);

    // Explode the file into pieces using this special marker
    $this->blocks = explode($marker, $contents);

    // Start breaking the blocks down (ignore the first block, just extra)
    for ($i = 1; $i < count($this->blocks); $i++)
    {
      // Explode the tags into an array
      $tags = explode($separator, trim(substr($this->blocks[$i], 0, strpos($this->blocks[$i], "\n"))));

      // Create a string that looks like an array
      $string = '';
      foreach ($tags as $tag)
      {
        $string .= "['$tag']";
      }
      $string .= "[] = $i";

      // Evaluate the string as a keyset and attach to $list
      eval("\$this->list$string;");
    }
  }
}

// Search keywords
$keywords = stripslashes(trim($_POST['search']));

// Search form
echo '<form method="post" >
 Search for an Article: <input type="text" name="search" value="' . $keywords . '">
<input type="submit" name="submit" value="Submit">
</form>';

// Create a new tag list
$file = new file_tags('terms.txt', '#------');

// Search for keywords in the list
$search = new key_search($keywords, $file->list);
?>

<pre>
<?php
//
// Display
//

// Display found matches
print_r($search->found);

// Loop through found matches
foreach ($search->found as $found)
{
  // Extract the tag for this match
  $tag = trim(substr($file->blocks[$found], 0, strpos($file->blocks[$found], "\n")));

  // Display the tag and its blocked content
  echo "\nTag: $tag\n" . strstr($file->blocks[$found], "\n");
  '</a><div style="margin-top:5px;margin-bottom:2em;border:1px black solid;">';
}

// Display the list
print_r($file->list);
?>
</pre>
When the user requests a search then I can show most of the matched terms (for some reason it doesnt like to find for example #------apple) and it sometimes shows too many on the browser at once.

vee


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Wouldn't this do the same?

Code: Select all

preg_match_all("/#-+$search([^#]*)/", $source, $matches);
where $search is a search string.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

a regular expression could help you there:

Code: Select all

$q = 'apple'

preg_match('/^#------('.$q.')\n(.*?)$/i',$file_contents,$matches);

//$matches[1] == 'apple'
//$matches[2] == 'A great fruit with many varieties.'
**edit - I see stereofrog beat me to it ;-) Great minds think alike!
killervee3
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 6:53 am

Post by killervee3 »

Sorry for not putting the code in php tags, Id swear I did that, o well : )

Surprisingly now it seems that it can actually search for all the terms, which is good news. :?

--------
Sorry frog the same as what?

Wouldnt preg_match_all display all the matches?, instead of snippets of the content 5ish terms at a time?
Would the -+ bit allow me to go through each of the matches found?
Post Reply