Author Topic: txt file search program needed  (Read 13614 times)

0 Members and 1 Guest are viewing this topic.

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #15 on: March 09, 2008, 10:36:12 AM »
It's working pretty good now, but I had a question on a couple small issues. Can it be adjusted to search for more than one word?
It certainly can, all you need to do is call the search() function fo each keyword entered in the search box.

And when you enter the search word and hit enter, it doesn't work, you have to use the mouse to click the button. Is this an easy issue to fix?
You'll need to use Javascript for that.

Offline FlusteredTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: txt file search program needed
« Reply #16 on: March 10, 2008, 07:49:48 AM »
sorry I'm so dense on this, but I don't quite understand what you mean by:
Quote
all you need to do is call the search() function fo each keyword entered in the search box.

Can you give me an example or something?  Thanks

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #17 on: March 10, 2008, 03:42:51 PM »
Modified the code:
Code: [Select]
<?php

function keywordSearch($keyword)
{
    
$results false;
    
$lines   file('search_data.txt');

    
$symbols = array('['']''-''('')');
    
$replace = array('\[''\]''\-''\(''\)');
    
$keyword str_replace($symbols$replace$keyword);

    foreach(
$lines as $line)
    {
        if(
eregi($keyword$line))
        {
            
$line str_replace($keyword"<b>$keyword</b>"$line);
            
$results[] = $line;
        }
    }

    return 
$results;
}

function 
displaySearchResults()
{
    global 
$keyword_results$keyword;

    
$output '';

    foreach(
$keyword_results as $keyword => $results)
    {
        
$output .= '<p>The search term "<i><b>'$keyword '</b></i>" found ';

        if(
is_array($results))
        {
            
$output .= count($results) . ' search result(s):';

            
$output .= '<ol><li>' implode('</li><li>'$results) . '</li></ol>';
        }
        else
        {
            
$output .= "no search results</p>\n\n";
        }
    }

    echo 
$output;
}

if(isset(
$_POST['submit']))
{
    if(!empty(
$_POST['keyword']) && strlen($_POST['keyword']) > 3)
    {
        
$_POST['keyword'] = str_replace(array("\r\n""\r""\n"), "\n"$_POST['keyword']);
        
$keyword_search explode("\n"$_POST['keyword']);

        if(
is_array($keyword_search))
        {
            foreach(
$keyword_search as $keyword)
            {
                if(
strlen($keyword) > 3)
                    
$keyword_results[$keyword] = keywordSearch($keyword);
            }
        }
        else
        {
             
$keyword_results[$keyword_search] = keywordSearch($keyword_search);
        }

        
displaySearchResults();
    }
    else
    {
        echo 
'Invalid search term';
    }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Keywords:<br /><textarea name="keyword" cols="40" rows="4"><?php echo isset($_POST['keyword']) ? $_POST['keyword'] : null?></textarea>
    <p><input type="submit" name="submit" value="Search" /></p>
</form>

Offline FlusteredTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: txt file search program needed
« Reply #18 on: March 13, 2008, 07:13:05 AM »
can it be modified to search for words more at random? I noticed that if you had a line of <one two three>, it would search and find "one two" and "two three", but no results if search was for "one three". Also because of the format of data, there are dots and astericks, so if the data line was <one.two*three>, there would be no results for searching "one two". I really appreciate the time and effort in helping with this, as I get totally lost.

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #19 on: March 17, 2008, 03:14:44 PM »
Hi, sorry I haven't replied. I have modified the script a little to how I think you wanted:
Code: [Select]
<?php

function keywordSearch(&$keywords)
{
    global 
$keyword_count;

    
$lines   file('search_data.txt');
    
$results false;

    
$symbols = array('['']''-''('')');
    
$replace = array('\[''\]''\-''\(''\)');

    
$colors  = array('#FF0000''#0000FF''#00FF00''#FFFF00');

    
$i 0;
    foreach(
$lines as $line)
    {
        
$keyword_found false;

        foreach(
$keywords as $key => $keyword)
        {
            if(!isset(
$keyword_count[$keyword]))
            {
                
$keyword_count[$keyword] = 0;
            }

            
$keyword str_replace($symbols$replace$keyword);

            if(
eregi($keyword$line))
            {
                
$keyword_found true;

                
// remove answer from end of line
                
$line eregi_replace("\*([a-z0-9 ]+)"'?' $line);
                
$line str_replace($keyword"<span style=\"color: {$colors[$key]}; font-weight: bold;\">$keyword</span>"$line);

                
$results[$i] = $line;

                
$keyword_count[$keyword]++;
            }
        }

        if(
$keyword_found)
            
$i++;
    }

    return 
$results;
}

function 
displaySearchResults()
{
    global 
$keyword_results$keywords$keyword_count;

    
$output  '<p>The keywords "<i><b>'implode('</b></i>", "<i><b>'$keywords) . '</b></i>" found ' count($keyword_results) . " result(s):\n";
    
$output .= "<ol>\n  <li>" implode("</li>\n  <li>"$keyword_results) . "</li>\n</ol>\n";

    echo 
"<p>$output</p>";
}

if(isset(
$_POST['submit']))
{
    if(!empty(
$_POST['keyword']))
    {
        
$_POST['keyword'] = str_replace(array("\r\n""\r""\n"), "\n"$_POST['keyword']);
        
$keywords explode("\n"$_POST['keyword']);

        
$keyword_results keywordSearch($keywords);

        
displaySearchResults();
    }
    else
    {
        echo 
'Invalid search term';
    }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Keywords:<br /><textarea name="keyword" cols="40" rows="4"><?php echo isset($_POST['keyword']) ? $_POST['keyword'] : null?></textarea>
    <p><input type="submit" name="submit" value="Search" /></p>
</form>

Offline FlusteredTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: txt file search program needed
« Reply #20 on: March 18, 2008, 10:04:29 AM »
There is no need for you to apologize, your help has been so greatly appreciated. I feel bad that I am so ignorant that I have to ask for so much help. The change doesn't seem to work however. It shows  1 blank result for each multi-word try. i.e.

The keywords "one three" found 1 result(s):

1.

Single word search still works


Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #21 on: March 18, 2008, 03:17:23 PM »
Bug fixed, forgot to add some in. Try:
Code: (search.php) [Select]
<?php

function keywordSearch(&$keywords)
{
    global 
$keyword_count;

    
$lines   file('search_data.txt');
    
$results = array();

    
$symbols = array('['']''-''('')');
    
$replace = array('\[''\]''\-''\(''\)');

    
// colors for highlighting keywords
    
$colors  = array( '#FF0000'// red
                      
'#0000FF'// blue
                      
'#99CC00'// green
                      
'#CCCC00'// yellow
                      
'#660066'// purple
                      
'#FF0099'  // pink
                    
);

    
$i 0;
    foreach(
$lines as $line)
    {
        
$keyword_found false;

        foreach(
$keywords as $key => $keyword)
        {
            if(!isset(
$keyword_count[$keyword]))
            {
                
$keyword_count[$keyword] = 0;
            }

            
$keyword str_replace($symbols$replace$keyword);

            if(
eregi($keyword$line))
            {
                
$keyword_found true;

                
// remove answer from end of line
                
$line eregi_replace("\*([a-z0-9 ]+)"'?' $line);

                
// highlight search keyword
                
$line eregi_replace("($keyword)""<span style=\"color: {$colors[$key]}; font-weight: bold;\">\\1</span>"$line);

                
$results[$i] = $line;

                
$keyword_count[$keyword]++;
            }
        }

        if(
$keyword_found)
            
$i++;
    }

    return 
$results;
}

function 
displaySearchResults()
{
    global 
$keyword_results$keywords$keyword_count;

    
$output  '<p>The keywords "<i><b>'implode('</b></i>", "<i><b>'$keywords) . '</b></i>" found ' count($keyword_results) . " result(s):\n";

    if(
is_array($keyword_results) && count($keyword_results) > 0)
    {
        
$output .= "<ol>\n  <li>" implode("</li>\n  <li>"$keyword_results) . "</li>\n</ol>\n";
    }

    echo 
"<p>$output</p>";
}

if(isset(
$_POST['submit']))
{
    if(!empty(
$_POST['keyword']))
    {
        
$_POST['keyword'] = str_replace(array("\r\n""\r""\n"), "\n"$_POST['keyword']);
        
$keywords explode("\n"$_POST['keyword']);

        
$keyword_results keywordSearch($keywords);

        
displaySearchResults();
    }
    else
    {
        echo 
'Invalid search term';
    }
}

?>

HTML
Code: [Select]
<form action="search.php" method="post">
    Keywords:<br /><textarea name="keyword" cols="40" rows="4"><?php echo isset($_POST['keyword']) ? $_POST['keyword'] : null?></textarea>
    <p><input type="submit" name="submit" value="Search" /></p>
</form>
Also note that I changed the search box to a textarea rather than a text field so you'll need to modify your search box HTML code to the HTML code posted above. When performing a search you now place each keyword on a separate line within the Keywords search box rather than a space.
« Last Edit: March 27, 2008, 05:44:09 PM by wildteen88 »

Offline FlusteredTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: txt file search program needed
« Reply #22 on: March 28, 2008, 10:30:51 PM »
sorry for delay, its real close, but there is one thing that would be better. On the multiple word search, it searches for either, and would be a lot better if it only searched for both (or multiple). For example, if you searched for "one" and "two" in search box, it shows all results with either word, instead of narrowing the search to only ones with both words.

Again, I want to thank you for the help and effort!!

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #23 on: March 30, 2008, 09:53:43 AM »
Umm not quite getting you. From my testing my code does this are you using my updated PHP and HTML code? Each search word must go on a separate line(rather than separated by a space) in the textarea.

Offline FlusteredTopic starter

  • Irregular
  • Posts: 24
    • View Profile
Re: txt file search program needed
« Reply #24 on: March 30, 2008, 01:33:07 PM »
What I was trying to say was if you search for multiple words, it gives results of any one word in different colors, but can be a large result page. I was wondering if it could be changed so it only gave results that only contained all of the search words used.

So if you had the following lines in the data and searched for "one two"(on separate lines), only the "one two three" line would  be a result

one two three
one three four
one three five

Currently it would show all three lines as a result with "one" in red, and "two" in blue



Offline nicob

  • Irregular
  • Posts: 40
    • View Profile
Re: txt file search program needed
« Reply #25 on: November 16, 2008, 11:03:14 PM »
For the webmasters working with this script...Do you know a solution for next "problem"?

My text file contains something like this (example is just a copy from Wikipedia):

Quote
Line (geometry), an infinitely-extending one-dimensional figure that has no curvature
a length of rope, cable or chain when put to use (such as a clothesline, anchor line)
a line or queue of people waiting in a queue area
a line of text in writing

When I search for 'figure', I get 'Line (geometry), an infinitely-extending one-dimensional figure that has no curvature' (cool!)

When I search for 'that has no curvature', I get 'Line (geometry), an infinitely-extending one-dimensional figure that has no curvature' (cool!)

BUT when I search for 'figure curvature', I get NOTHING. So this script is search for the exact match.

Is there a way to tell this script to show the lines with 'figure' and 'curvature' in the same line?

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #26 on: November 17, 2008, 01:22:02 PM »
If you're using this version here
Then enter each word to search for on a separate line.

Offline nicob

  • Irregular
  • Posts: 40
    • View Profile
Re: txt file search program needed
« Reply #27 on: November 17, 2008, 02:47:08 PM »
If you're using this version here
Then enter each word to search for on a separate line.
Yes, I'm using that version. But now I understand why I didn't understand that part of search:
- When you have let's say 10 lines with 'figure' and 'curvature' not all words get a red or blue color (when on the same line, in some cases only one word gets a color).
- Because I'm using GET (and not POST) I see an url like .../search.php?keyword=figure%0D%0Acurvature&submit=Search. I expected '+'.

I have tested out alot of search-in-files-script, but this is the best: simple to use, great options, and no database required :D

Some technical questions for coders:
1. How do you convert '%0D%0A' to '+' in the url?
2. I'm trying to include the result in a page in a local format
Code: [Select]
<?php include "/home/account/domains/domain.com/public_html/search.php?keyword=garden&submit=Search"?>
but this is not working, so now I'm using fopen to reach the same result. But I have to use 'http'. Is there a solution for this?
3. is it possible to remove '&submit=Search' from the output url?
« Last Edit: November 17, 2008, 02:50:03 PM by nicob »