0

Sorry, I may not have worded that properly...

For this example, the text file will be called example.txt. Also, the value will hereby be named "Apple"

What I am trying to do is take example.txt, open it, then find Apple and display the entire line that value rests on.


example.txt


The fastest land animal is a Cheetah.

The name of a small table is a Desk.

The name of a red fruit is Apple.

The maker of my car is Toyota.

My favorite gun is a Shotgun.


Webpage


[Line Displays Here]


I've tried MANY solutions to this (google is not my friend in this case. How dare you google!) I'll post some of them to show you what I have tried. Keep in mind, I am still a bit new to php. I believe the farthest I got was opening the files and displaying an array....

    $file_handle = fopen("dictionary.txt", "rb");

    while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode('=', $line_of_text);

    print $parts[0] . $parts[1]. "<BR>";

    }

    fclose($file_handle);

This did not work...

Nor did this

$lines = file('file.txt');

$output = '';
$TorZ = "Array
(";
$num = 0;
foreach ($lines as $line)
{
    $output .= $TorZ.' '.$line.PHP_EOL;
    $TorZ = '['.$num.'] =>';
    $num++;
}

file_put_contents('output.txt', $output);

Or this..

$result = array();
$file = explode("\n", file_get_contents("file.txt"));
foreach ( $file as $content ) {
    $result[] = array_filter(array_map("trim", explode("\n", $content)));
}
var_dump($result);

Any ideas?

Also, I sincerely apologize if this has been answered in the past, I spent about 2 hours searching for a way to do this, and still cannot find anything.

1 Answers1

0

I found the answer I was looking for after spending a little more time searching and came up with this result. Works just like I wanted it to! PHP to search within txt file and echo the whole line

Community
  • 1
  • 1