PHP Read Line From Text File, Only Selected Line - php

I am trying to search a text file for a string, and then display only the one line that was found. Would like to display the found line in a label box. My PHP is really weak. I got the following code working which will display the first line in the text area. Below is the contents of the text file: test.txt
First line
Second Line
Third Line
Fourth Line
My code is as follows:
<!DOCTYPE html>
<html>
<body>
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>
</body>
</html>
This code returns First Line into the text area, so how can I return First Line to
text box, label?
Any pointer toward the right direction?
Thanks
Mitch

You'd want to continue fgets($file) in a loop, and checking if the search string is in the result, then when the search string is found, echo the line. Something like this maybe...
while ( $line = fgets( $myfile ) ) {
if ( str_contains( $line, $search_string ) ) {
echo $line;
break;
}
}
Note that str_contains is php 8 and up only. There are other methods for lower versions.
Also consider that perhaps a database would be better for this type of operation rather than using a flat file...

Related

How to show php string in html input

I have a flat file TestFile.txt that has about 40 lines of data, each item on a separate row, so 40 lines. I have a PHP code that finds the row containing a string I want to find using using $Search_String. Then displays only the row containing $Search_String. This works exactly as I want. However; it displays the result in text area. How do I display the result into a label box? The php reads the flat just as I would expect. I have 2 html input fields, but the string $line only contains data inside the if statement. Any help with this would be great!
Here is part of my flat file, filename is TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=8Yg81xrqK0313zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Here is my PHP:
<!DOCTYPE html>
<html>
<body>
<?php
$line = '';
?>
<input type="text" ID="message" value="<?php echo $line;?>"/>
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "RXF";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo $line;
echo $Search_String;
}
}
// Properly close the text file.
fclose($MyFile);
?>
<input type="text" id="message" value="<?php echo $line;?>"/>
</body>
</html>
This code does properly open the flat text file, it does the search and finds
the line containing the data I am trying to retrieve. I see the correct gata using echo in the if statement. Now I need to place the found data into the html input text box. The string $line seems to contain no data outside the if block of code. Can I make the variable $line static or global so the data is available throughout the code? Or am I using the html input text improperly? I think I do need to use input text, because I want to be able to modify the data and then write the line back to the flat file.
Thanks for any suggestions,
Mitch
This is a small grasp of your code including the proposed solution to loop through and show all the lines matching the criteria from your file:
<?php
/*...*/
$lines = [];
while ( $line = fgets( $MyFile ) ) {
if ( str_contains( $line, $Search_String ) ) {
//here you are keeping track of each line matching criteria
$lines[] = $line;
//echo $line;
//echo $Search_String;
}
}
?>
<?php foreach($lines as $line): ?>
<input type="text" value="<?= $line; ?>"/>
<?php endforeach; ?>

PHP write to flat text file. New to PHP, learning. I have the read working. Need to write back updates

I have a flat file, TestFile.txt, that contains about 200 lines. Each item is a separate row. I show a partial of the contents of the TestFile.txt file below. I have PHP code working that reads TestFile.txt exactly as I need. The PHP read code searches the TestFile.txt, locates the line I wish to read, and places the result into an html input box. It parses the text after the = in the line, and only displays the data found after the =. Just as I need. Now I need to change the data in the html input box, and write the change back to TestFile.txt, and only update the text after the =. I show the PHP read code below. I have not a clue how to do what I need. I am a little over a week studying PHP. Any help with writing is much appreciated.
Thanks,
Mitch
Partial TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=9Yg81prqL0363zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Part of the PHP:
<!DOCTYPE html>
<html>
<body>
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String_1 = "RXOff";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
/*...*/
// Next, let us build the array.
$lines = [];
while ( $line = fgets( $MyFile ) ) {
if ( str_contains( $line, $Search_String_1 ) ) {
//here you are keeping track of each line matching the criteria.
$lines[] = $line;
// This explode function will split the string contained
// in the $line variable at the =. Text left of the = is
// placed into the $key variable. Text right of the = is
// placed into the $value variable.
[$key, $value] = explode("=", "$line");
// echo $key; // RXOffset;
// echo $value; // 260;
//echo $line;
//echo $Search_String_1;
}
}
?>
<?php foreach($lines as $line): ?>
<?php endforeach;
// Properly close the text file.
fclose($MyFile);
// Get string $value from the explode code above.
?>
<label>RXOffset: <input type="text" id="message" value="<?php echo $value;?>"/></label>
<?php
</body>
<html>
Hope this gives enough information. Feel free to comment questions.
Thanks,
Mitch
This is what appears on the browser when I execute this PHP:
RXOffset: 269
Label Data

How to write response into label box

I have a flat file TestFile.txt that has about 40 lines of data, each item on a separate row, so 40 lines. I have a PHP code that finds the row containing a string I want to find using using $Search_String. Then displays only the row containing $Search_String. This works exactly as I want. However; it displays the result in text area. How do I display the result into a label box?
Here is part of my flat file, filename is TestFile.txt:
RXFrequency=432675000
TXFrequency=432675000
RXOffset=260
TXOffset=120
Network=mnet.hopto.org
Password=8Yg81xrqK0313zt
Latitude=34.657783
Longitude=-3.784595
Port=62021
Here is my PHP:
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "Lon";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo $line;
break;
}
}
// If the string $Search_String was not found, show a message
if(!$found)
{
echo 'No match found';
}
// Properly close the text file.
fclose($MyFile);
?>
The PHP code above returns:
Longitude=-3.784595
Just what I want, but I need it displayed in text box, or label box.
You need to print the answer as html
<?php
// Place text to look for in string $Search_String.
// The $Search_String will remain hard coded in my production
// code. The users will not be able to select $Search_String.
$Search_String = "Lon";
// Identify Text File, open File and Read the File.
$MyFile = fopen("TestFile.txt", "r") or die("Unable to open file!");
$found= "False";
// Create the while loop. Test each line with the if statement,
// looking for $Search_String, and place the result into string $line.
// Next, echo string $line which containes the found line in the
// flat text file. It will return the entire line even from a
// partial $Search_String, which is what I want.
while ( $line = fgets( $MyFile ) )
{
if ( str_contains( $line, $Search_String ) )
{
echo "<div style="
border: double;
">".$line."</div>";
break;
}
}
// If the string $Search_String was not found, show a message
if(!$found)
{
echo 'No match found';
}
// Properly close the text file.
fclose($MyFile);
?>

How do I read a text file line by line?

suppose I do have a text file with these lines
name: Mathew
Age : 32
Country : USA
Location : California
bla bla bla....
What I want is I want a php code which can read this file and display result to a webpage.
Use this code (untested):
$fp = fopen('filename.php');
while (!eof($fp)) {
$line = fgets($fp);
// Add code to display the values how you want
echo $line."<br>";
}
fclose($fp);
That will loop through the file line by line. Each line will be assigned to the $line variable, and then you can manipulate and display the values how you would like.
file() function reads a file into an array where one element represents a string in the file
Display the actual text or remove the name:, etc?
Use the file() function (tutorial?) to read the file in and then echo out / process each line.

Read Data From Text File PHP

I'm just wondering how I can read a text file in php, I'd like to have it display the last 200 entries (their each on a new line) from the text file.
Like
John White
Jane Does
John Does
Someones Name
and so on
Thanks!
Use fopen and fgets, or possibly just file.
There are several methods for reading text from files in PHP.
You could use fgets, fread, etc. Load the file into a dynamic array, then just output the last 200 elements of that array.
file will get the contents of a file and put it into an array. After that, it's like JYelton said, output the last 200 elements.
This outputs last 200 rows. Last row first:
$lines = file("filename.txt");
$top200 = array_slice(array_reverse($lines),0,200);
foreach($top200 as $line)
{
echo $line . "<br />";
}
<?php
$myfile = fopen("file_name.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
You may use this

Categories