Jump to content

Searching a small textfile


Guest aceooo2

Recommended Posts

Guest aceooo2

Hey all, I wondered if anyone could 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 (which does output the terms):-

 

<?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);

?>


<?php
//preg_match_all("/#-+$search([^#]*)/", $source, $matches);
?>



<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 it shows too many on the browser at once. (When I said small it can still contains around 50+ terms). Thats why snippets of 5 at a time would be nice.

 

 

Also it shows what array has what term below, which I need to get rid of for uglyness lol:-

 

Array

(

    [apple] => Array

        (

            [0] => 1

            [granny smith] => Array

                (

                    [0] => 2

                    [Jersey] => Array

                        (

                            [0] => 19

                        )

 

                    [england] => Array

                        (

                            [0] => 20

                        )

 

etc..

 

Ill update if I complete something, but any help?

Link to comment
https://forums.phpfreaks.com/topic/47434-searching-a-small-textfile/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.