Jump to content

Search .txt file


davemoody

Recommended Posts

Hello,

 

I've a need to search content in a text file, each line delimited with a |

 

I found a simple search script which works fine, but only gives confirmation if successful or not, and it is case sensitive. I need to modify it so that it is not case sensitive, and to return the whole line, each line that contains the text, in a table on the page.

 

Here's my code:

 

<?php

// Basic search script sourced from:

// http://www.000webhost.com/forum/web-programming/8728-php-help-search-multiple-strings-text-file.html

 

echo '<form method="post" action=""><input type="text" name="search" size="50" value=""  /></form>';

 

if(!$_POST['search'] == ''){

$file = file_get_contents("products/products.txt");

$searchstrings = $_POST['search'];

$breakstrings = explode(',',$searchstrings);

 

foreach ($breakstrings as $values){

  if(!strpos($file, $values)) {

echo $values." string not found!\n";

  } else {

  echo $values." string Found!\n";

  }

}

}

?>

 

Is this doable, or can someone recommend a better script?

 

Thanks in advance,

Dave.

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/
Share on other sites

Case sensitivity solved:

 

<?php

// Basic search script sourced from:

// http://www.000webhost.com/forum/web-programming/8728-php-help-search-multiple-strings-text-file.html

 

echo '<form method="post" action=""><input type="text" name="search" size="50" value=""  /></form>';

 

if(!$_POST['search'] == ''){

$file = file_get_contents("products/products.txt");

$searchstrings = $_POST['search'];

$breakstrings = explode(',',strtolower($searchstrings));

 

foreach ($breakstrings as $values){

  if(!strpos(strtolower($file), $values)) {

echo $values." string not found!\n";

  } else {

  echo $values." string Found!\n";

  }

}

}

?>

 

 

Just need to output each line in a table now. Any ideas on how to do that?

 

Dave.

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256174
Share on other sites

try something like this: (untested, may still have bugs)

 

<?php
echo '<form method="post" action=""><input type="text" name="search" size="50" value=""  /></form>';

   if(trim($_POST['search']) != ''){
      $file = strtolower(file_get_contents("products/products.txt"));
      $searchstrings = trim($_POST['search']);
      $breakstrings = explode(',',$searchstrings);

      foreach ($breakstrings as $values){
      	$lines = explode("|",$file);
      	foreach($lines as $line){
        	if(strpos($file, $values)){
        		echo $values." string Found in ".str_replace($values,"<b>".$values."</b>",$line)."<br>\n";
        	}
        }   
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256175
Share on other sites

try this one:

 

<?php
echo '<form method="post" action=""><input type="text" name="search" size="50" value=""  /><input type="submit" value="test"></form>';

   if(isset($_POST['search']) && trim($_POST['search']) != ''){
      $file = file_get_contents("products/products.txt");
      $searchstrings = trim($_POST['search']);
      $breakstrings = explode(',',$searchstrings);
$shown = array();
      foreach ($breakstrings as $values){
      	$lines = explode("|",$file);
      	foreach($lines as $k=>$line){
        	if(stristr($line, $values) && !in_array($k,$shown)){
        		echo " string Found in '".str_replace($values,"<b>".$values."</b>",$line)."'<br>\n";
        		$shown[] = $k;
        	}
        }   
      }
   }
?>

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256180
Share on other sites

Thanks, this is definately moving forward. I'm trying to understand how the code is working, not easy for a beginner like me!

 

Here's a sample line from the text file:

 

hillsong|<img class="portrait3" src="products/ABeautiful.jpg" alt="CD: A Beautiful Exchange" />|A Beautiful Exchange. <p />Hillsong's main album for 2010, loaded with some of their best songs to date...|$24.99|CD|products_hillsong_abeautiful.php|4

 

I need to pull out the link and make it clickable...

 

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256183
Share on other sites

This is what I've done with your code:

 

<?php

// Basic search script sourced from:

// http://www.000webhost.com/forum/web-programming/8728-php-help-search-multiple-strings-text-file.html

 

echo '<form method="post" action=""><input type="text" name="search" size="50" value=""  /></form>';

 

if(isset($_POST['search']) && trim($_POST['search']) != ''){

echo "<br /><br />";

echo "<table>";

echo "<tr><th colspan='4'> </th></tr>";

$file = file_get_contents("products/products.txt");

$searchstrings = trim($_POST['search']);

$breakstrings = explode(',',$searchstrings);

$shown = array();

foreach ($breakstrings as $values){

$lines = explode("|",$file);

foreach($lines as $k=>$line){

if(stristr($line, $values) && !in_array($k,$shown)){

echo "<tr><td>$lines[1]</td><td>$lines[2]</td><td class='price'>$lines[3]</td><td class='center'>$lines[4]<p /><br /><a href='$lines[5]'><input class='button' type='button' value='View details'/></a></td></tr>";

}

}

echo "</table>";

echo "<p /><div class='mandatory'>Unless otherwise stated all prices are given in Australian Dollars</div>";

}

?>

 

I successfully search items, but if for example there is 3 response, then it lists the first item in the text file three times... Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256189
Share on other sites

since the links are on their own line, and are php pages, you could just use something like:

 

// search for .php matches so I know it's a page name:
if(strpos($line,".php"){
    // php page found:
    echo "this should be a link to this page: <a href='".$line."'>".$line."</a>";
}else{
// not a page name
}

Link to comment
https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256320
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.