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

0 Members and 1 Guest are viewing this topic.

Offline FlusteredTopic starter

  • Irregular
    • View Profile
txt file search program needed
« on: March 08, 2008, 05:33:23 AM »
I need a search engine for a web site that will search a .txt file and return each line as a result. I used a program called Search Engine Builder Pro v2.02, that came close, but it detects the .txt file as one big result, it doesn't read each line as a result. Any ideas? Thanks all!

P.S. If needed, I could put up a temp web site that would show what I have so far and what isn't working

P.S.S. I am an idiot on any scripting, so please make any answers in a manner that a 6 yr old would understand...lol (and sorry for any offense to 6 yr olds that may read this)
« Last Edit: November 16, 2008, 08:33:29 AM by wildteen88 »

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #1 on: March 08, 2008, 07:03:12 AM »
Post some example data from the text file and the result you'd like.
« Last Edit: November 16, 2008, 08:33:32 AM by wildteen88 »

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #2 on: March 08, 2008, 07:41:27 AM »
Below are some lines from the type of txt. What I need is is you searched for "Followers", it would return 2 results (as I duplicated the line). What I have now, just shows the entire .txt as 1 result. If it finds the search word, I need it to return as a result, the entire line it is in as 1 result. For example:
Found 1 result:
Followers of the Unification Church are called ________*Moonies
Found 2 result:
Followers of the Unification Church are called ________*Moonies

I think it would need some way to block certain searchs, like single letters or numbers, so it doesn't go crazy





Followers of the Unification Church are called ________*Moonies
In which state is Mount St. Helens*Washington
Astronomy: Which planet does the moon Io belong to*Jupiter
Name Alley Oop's girl friend*Oola
What war lasted from june 5 to june 11, 1967*six day war
Basketball: The Seattle ________*Supersonics
What does a male cow have to undergo if it's being raised for beef*Castration
What is the art and science of mapmaking called*Cartography
Where is Queen Maud Land located*Antarctica
Kaos Most popular christmas shows in 2007*
11/27/2007     Freakin.Christmas   1903
11/18/2007     Stole.Christmas   1253
11/12/2007     Christmas.With.The.Sim   401   6765
11/22/2007     Christmas.Time   47400
11/22/2007   Times   4768         
Followers of the Unification Church are called ________*Moonies
In which state is Mount St. Helens*Washington
Astronomy: Which planet does the moon Io belong to*Jupiter
Name Alley Oop's girl friend*Oola
What war lasted from june 5 to june 11, 1967*six day war
Basketball: The Seattle ________*Supersonics
Subject, verb and object are parts of a _________*Sentence
This animal is found at the beginning of an encyclopedia*ardvark
In what country can the Big Merino, Big Murray Cod, Big Pineappe, and Big Worm be found in*Australia
Which kind is the DEEPEST sea*Coral Sea
A line drawn from an angle of a triangle to the mid-point of the opposite side is a(n) _______*Median
How long is the Le Mans Endurance motor race*Twenty four hours
Famous medical scale that reports the grade of consciousness of a patient*Glasgow
What is terebinth*turpentine tree
Frankish ruler Charles the Great is better known as _________*Charlemagne
Name Li'l Abner's favorite Indian drink*Kickapoo Joy Juice
Music: What year was Elvis Presley born*1935

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #3 on: March 08, 2008, 08:19:13 AM »
Something like this:
Code: [Select]
<?php

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

    
// escape characters which are sensitive to eregi()
    
$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;
}

if(isset(
$_POST['submit']))
{
    if(!empty(
$_POST['keyword']) && strlen($_POST['keyword']) > 3)
    {
        
$keyword        $_POST['keyword'];
        
$search_results search($keyword);

        if(
is_array($search_results))
        {
            echo 
'The search term "<i><b>'$keyword '</b></i>" found ' count($search_results) . ' search result(s):';

            echo 
'<ol><li>' implode('</li><li>'$search_results) . '</li></ol>';
        }
        else
        {
            echo 
'No results was returned';
        }
    }
    else
    {
        echo 
'Invalid search term "<i><b>'$_POST['keyword'] . '</b></i>"';
    }
}

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    Search word: <input type="text" name="keyword" /> <input type="submit" name="submit" value="Search" />
</form>
search_data.txt is the file which contains your search data.
« Last Edit: March 08, 2008, 08:27:55 AM by wildteen88 »

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #4 on: March 08, 2008, 08:42:48 AM »
I'm getting an error, as this part of the code shows on the webpage and also I'm guessing its stopping any results from showing:
Code: [Select]

3) { $keyword = $_POST['keyword']; $search_results = search($keyword); if(is_array($search_results)) { echo 'The search term "'. $keyword . '" found ' . count($search_results) . ' search result(s):'; echo '

' . implode('
', $search_results) . '
'; } else { echo 'No results was returned'; } } else { echo 'Invalid search term "'. $_POST['keyword'] . '"'; } } ?>



Thanks so much for helping!

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #5 on: March 08, 2008, 08:50:40 AM »
Huh, don't understand you there, why is the code in one line? What errors are you getting? If code is being displayed then something is wrong with your PHP installation.

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #6 on: March 08, 2008, 08:59:23 AM »
when I put that code on a html page, I see Search Word: and the search box, but above that I see the above part of the code in text on the page

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #7 on: March 08, 2008, 09:04:32 AM »
You need to save the code within a .php file and not a .html file. Make sure your hosting account supports php.

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #8 on: March 08, 2008, 09:14:58 AM »
sorry I'm so dense, but what do I need to add to html page to link to this?

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #9 on: March 08, 2008, 09:22:57 AM »
As in a hyperlink?
Code: [Select]
<a href="search.php">Search</a>Or type go to it manually:
http://yoursite.com/search.php

Change search.php to the name of your actual .php file.
« Last Edit: March 08, 2008, 09:23:32 AM by wildteen88 »

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #10 on: March 08, 2008, 09:32:22 AM »
not as a hyperlink, I mean I don't have the search box showing on the html page

Online wildteen88

  • PHPFreaks Recommended
  • 'Insane!'
  • *
  • Gender: Male
    • View Profile
Re: txt file search program needed
« Reply #11 on: March 08, 2008, 09:36:27 AM »
If you want the search box on the html page then you can copy the form code over to your html page, set the action attribute in the form tag to search.php, eg:
Code: (search.html) [Select]
<form action="search.php" method="post">
    Search word: <input type="text" name="keyword" /> <input type="submit" name="submit" value="Search" />
</form>
When the form is submitted the browser will load search.php displaying the results.

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #12 on: March 08, 2008, 10:16:37 AM »
Thanks so much for the help, with a bit of luck, I can get this working now!!

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #13 on: March 08, 2008, 02:05:24 PM »
ty
« Last Edit: March 08, 2008, 02:09:23 PM by Flustered »

Offline FlusteredTopic starter

  • Irregular
    • View Profile
Re: txt file search program needed
« Reply #14 on: March 08, 2008, 03:42:27 PM »
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?  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?

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.