title: Searching for a keyword in a text file
body: <p>I'm working on a PHP search script that scans a text file repository of packages on a website and returns the URL of each match. The text files looks as followed:</p>
  
  <pre><code>PACKAGE NAME: package_one
  PACKAGE LOCATION: ./package_one
  PACKAGE VERSION: 0.1.2
  
  PACKAGE NAME: package_two
  PACKAGE LOCATION: ./package_two
  PACKAGE VERSION: 0.2.1
  
  PACKAGE NAME: package_three
  PACKAGE LOCATION: ./package_three
  PACKAGE VERSION: 1.3.4
  
  PACKAGE NAME: package_one
  PACKAGE LOCATION: ./package_one
  PACKAGE VERSION: 0.1.2
  </code></pre>
  
  <p>My search script currently looks as followed:</p>
  
  <pre><code>&lt;?
  $search = $_POST['search']; //Passed from a user input form
  
  $data_file = file('http://www.example.com/data_file.txt'); //The text file repository
  
      $final_concatenated_results = "";
  
      for ($i = 0, $found = FALSE; isset($data_file[$i]); $i++) {
          if (trim($data_file[$i]) === $search) { 
          $cleanse = trim($data_file[++$i]);
          $output_part_one = strstr($cleanse, '.');
          $output_part_two = "http://www.example.com/packages_repository/".$output_part_one;
          $add_this = "&lt;a href='$output_part_two'&gt;$output_part_two&lt;/a&gt;"."&lt;br/&gt;";
          $found = TRUE;
          $final_concatenated_results.=$add_this;
      } }
  
      if ($found == TRUE) {
          echo $final_results;
      }
  
      if ($found == FALSE) {
          echo "Error, package not found.";
      }
  ?&gt;
  </code></pre>
  
  <p>For example, if "PACKAGE NAME: package_two" were typed in verbatim this would be the output -- <a href="http://www.example.com/packages_repository/./package_one" rel="nofollow">http://www.example.com/packages_repository/./package_one</a> which works fine. </p>
  
  <p>Ideally, I'd like to know how to change my code so instead of typing in "PACKAGE NAME: package_two", one could simply type in "package_two" or even some like "two" or "tw". How much tweaking would that need?</p>
  
  <p>Also, I made a variable to concatenate all of the resulting URLs to one string, so if "package_one" were searched, it would return two URLS... I don't think I did the code right as it only produces the one URL.</p>
  
  <p>** I should mention that immediately after the package name is matched, it finds the next string following "PACKAGE LOCATION: ." to grab the URL</p>
  
  <p>Any input is appreciated, thank you!</p>