How do I read a text file line by line? - php

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.

Related

add string to text file with verification

I have a code:
$fp = fopen("/path/to/file", "w");
fwrite($fp, $var);
fclose($fp);
I need to do - add text string write in a text file without spaces with verification sample text.txt
foo
bar
foo_bar
If foo already exist in file - nothing to add.
Add a line of text in a file, but with check, if text is already there, example foo there is then nothing to add. If i add foofoo add it to text.txt
One way is to read into an array and check for the value. If it doesn't exist, append it (with a newline \n):
$lines = file("/path/to/file", FILE_IGNORE_NEW_LINES);
if(!in_array($var, $lines) {
file_put_contents("/path/to/file", "\n$var", FILE_APPEND);
}

how to delete a specific line from a file starting a string in php

I have a text file names data.dat containing space separated strings on each line. I want to delete a whole line starting with a specific from it, Please provide tested php code for it. I'm using php 5.4+
File Contents :
abc samle sample
this abc sample
xyz test sample sample
For example, I have $str="this". So in this case I want to delete 2nd line, For general that could occur at first line or middle or end. Main thing is I dont want any empty line. So new file should be
abc samle sample
xyz test sample sample
Try with this:
<?php
$f = "data.dat";
$term = "this";
$arr = file($f);
foreach ($arr as $key=> $line) {
//removing the line
if(stristr($line,$term)!== false){unset($arr[$key]);break;}
}
//reindexing array
$arr = array_values($arr);
//writing to file
file_put_contents($f, implode($arr));
?>

Detecting a line break & invoking a new paragraph on the output in PHP

So I have this snippet which gets my input from textarea named "cdet" & opens "index.php" & finds string "details" & replace it with my input-
if(ISSET($_REQUEST["sub"])){
$cdet=$_REQUEST["cdet"];
$fname = "index.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace("details", $cdet, $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
}
fclose($fhandle);
& this is the part in "index.php" where the string "details" is-
<p class="wNote">details</p>
What I want is that if a line break/new line occurs in the input, I would end the current & invoke a new one for the new line...
e.g- if input is
Hello there..
What are you doing here?
then details should be replaced like-
<p class="wNote">Hello there..</p>
<p class="wNote">What are you doing here?</p>
First, you can load file the easier way, with file_get_contents() function:
http://php.net/manual/en/function.file-get-contents.php
Then, after you get that $cdet field value use explode() function to split it by "\n" sign (new row). That way you'll get an array that contains rows of text.
Then iterate trough that array (with foreach() ) and for every row add that '<p class="wNote">', then row content and then '</p>'.
At end you can't just replace that 'details' words with your result, but you must replace whole '<p class="wNote">details</p>' with your output, because you can have more than one row now.

How to update a .txt file using PHP

My colleague and I are working on a chat application for a small Flash based game. We would like to keep our chat file as small as possible by automatically deleting old text after the file has reached a certain limit. Say the file exceeds 50 lines, we would like to delete the existing information and begin again at line 1. Is this possible?
<?php
$file = "saved.txt";
$edited_text = $_POST['new_text'];
$open = fopen($file, "a+");
fwrite($open, "\n" . $edited_text);
fclose($open);
?>
Basically something like this:
$lines = file('saved.txt');
$lines[] = 'new line of text';
array_unshift($lines); // remove first array element
file_put_contents('saved.txt', implode(PHP_EOL, $lines));
Read the file into an array, one line per array element
Append your new line(s) of text
Remove as many lines from the start of the array as necessary
dump array back out to file as text.
This would work:
// Read entire file
$lines = file_get_contents('saved.txt');
// Add what you want to the beginning of array
array_unshift($lines, 'new line of text');
// Keep first 50 items
$lines = array_splice($lines, 0, 50);
// Write them back
file_put_contents('saved.txt', implode(PHP_EOL, $lines));
Will always keep the first 50 elements intact (which includes messages from new to old).

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