davemoody Posted August 12, 2011 Share Posted August 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/ Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256174 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 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"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256175 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 Thanks, but your solution only works for single words, not phrases. Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256179 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 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; } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256180 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256183 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256189 Share on other sites More sharing options...
davemoody Posted August 12, 2011 Author Share Posted August 12, 2011 My modification really messed things up, gone back to yours now, but need to get it to produce a clickable link. Dave. Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256197 Share on other sites More sharing options...
WebStyles Posted August 12, 2011 Share Posted August 12, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/244558-search-txt-file/#findComment-1256320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.