Function for opening file - php

I have a code like this.
but the problem is why can't output ... please give suggestions, what is wrong with this code
<?php
function randLine($fileName) {
$fh = #fopen($fileName, "r");
while (true) {
$line = fgets($fh);
if ($line == null) {
break;
}
}
fclose($fh);
return $line;
}
$tes = randLine("list.txt");
$tes = str_replace(array("\n", "\r"), "", $tes);
echo $tes;

Related

Trying to write content to a file I've created, it reads all 3 lines, but when I change the lines to read 2 it still prints out 3?

$my_content = "This is the first line\n
This is the second line\n
This is the third line\n";
$my_filename = "save.txt";
function file_writer(string $file_to_write, string $content_to_write){
$file = fopen($file_to_write, "w") or die("Unable to open file");
file_put_contents($file_to_write, $content_to_write);
fclose($file);
}
file_writer($my_filename, $my_content);
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
while(! feof($file))
{
$line = fgets($file);
echo $line;
}
}
**file_reader($my_filename, 3);**
Try this:
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
$c = 0;
while(! feof($file) && $c != $num_lines)
{
$c = $c+1;
$line = fgets($file);
echo $line;
}
}
Your other problem is that you have newlines after newlines.
$my_content = "This is the first line\nThis is the second line\nThis is the third line\n";
function file_reader(string $file_to_read, int $num_lines) {
$file = fopen($file_to_read, "r");
$currLineNo = 1;
while(!feof($file) && (currLineNo < $num_lines))
{
$line = fgets($file);
echo $line;
$currLineNo += 1;
}
}
Havent tried the code myself. This is roughly way you can stop the loop at arbitrary line number.

how to replace one line of a file with php

I want to replace one line of a file with php, how can i do it?
This is the code where i print the line that I want to replace:
$file = fopen("file.dat", "a+");
$eqs = file_get_contents("file.dat");
$eqs = preg_split( "/\n/", $eqs );
foreach ($eqs as $valor) {
if(strpos($valor, $sn) !== false){
echo $valor; //this is the line to replace
} else{
echo "Not found";
}
}
Thanks in advance
As deceze♦ mentioned, unless the line is of fixed length, the easiest way is to process the whole file and output it to a new file.
Create a variable $newdata to append the processed data.
If your strpos statement !== false then you can change that text with the $replace_text variable and append that instead.
Once the loop has finished save your output to a new file. (If PHP has the appropriate permissions)
$file = fopen("file.dat", "a+");
$eqs = file_get_contents("file.dat");
$eqs = preg_split( "/\n/", $eqs );
$newdata = "";
foreach ($eqs as $valor) {
if(strpos($valor, $sn) !== false){
echo $valor; //this is the line to replace
$replace_text = "test";
$newdata = $newdata.$replace_text."/\n/";
} else{
$newdata = $newdata.$valor."/\n/";
}
}
$myfile = fopen("newfile.dat", "w") or die("Unable to open file!");
fwrite($myfile, $newdata);
fclose($myfile);
I would suggest this solution because it saves resources by processing line by line.
$source = fopen('file.dat', "r");
$target = fopen('file.dat.tmp', "w");
while ($line = fgets($source)) {
if(strpos($line, $sn) === false){
fputs($target, $line);
}
}
fclose($source);
fclose($target);
unlink('file.dat');
rename('file.dat.tmp','file.dat');

i want to store readed file output in variable

in PHP I want each string read by "fgets()" function in a separate variable.
Below is my code
$handle = #fopen("txtfile.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
An output of this code is:
I am Demo
I want this output as:
$word1 = I
$word2 = am
$word3 = Demo
<?php
$handle = #fopen("txtfile.txt", "r");
$text = '';
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$text .= $buffer;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
$array = explode(' ', $text);
$i = 1;
foreach ($array as $word) {
${"word$i"} = $word;
$i++;
}
Now you have each word saved in a different var

PHP code read and writes to file fine UNTIL i compare a substring

The following PHP code works to read one file and write the same content into another file:
<?php
$input = #fopen("./temp.def", "r");
$output = #fopen("./temp.tpc","w");
if ($input) {
while (!feof($input)) {
$buffer = fgets($input);
fputs($output, $buffer);
}
if (!feof($input)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($input);
fclose($output);
}
But when I add code to replace some content based off of whether or not a substring is some specific text it doesn’t work. Here’s the code for that:
<?php
$input = #fopen("./temp.def", "r");
$output = #fopen("./temp.tpc","w");
if ($input) {
while (!feof($input)) {
$buffer = fgets($input);
//check to see if it is a `row` line
$isrow = mb_substr($buffer, 0, 5, 'UTF-8');
if ($isrow == "row=:") {
$data = str_ireplace("row=:", "base=:[loc^nb]" . chr(10) . "row=:", $buffer);
else {
$data = $buffer;
}
}
fputs($output, $data);
}
if (!feof($input)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($input);
fclose($output);
}
The only thing changed in the code is the added part, highlighted in yellow, and the variable name change, highlighted in blue...
color-highlighted code
You have some weird bracing going on there. Try:
if ($isrow == "row=:") {
$data = str_ireplace("row=:", "base=:[loc^nb]" . chr(10) . "row=:", $buffer);
} else {
$data = $buffer;
}

read a text file with PHP and display all text after a certain string

I have a text file with lots of text, and I want to display a part of it on the screen with PHP.
At a certain point there's string like ITEM DESCRIPTION:
What I want to get all content from this string (just after it) to the end of the file.
This is my code so far:
$file = "file.txt";
$f = fopen($file, "r");
while ($line = fgets($f, 1000))
echo $line;
:)
How about you use strstr() and file_get_contents() ?
$contents = strstr(file_get_contents('file.txt'), 'ITEM DESCRIPTION:');
# or if you don't want that string itself included:
$s = "ITEM DESCRIPTION:"; # think of newlines as well "\n", "\r\n", .. or just use trim()
$contents = substr(strstr(file_get_contents('file.txt'), $s), strlen($s));
$file = "file.txt";
$f = fopen($file, 'rb');
$found = false;
while ($line = fgets($f, 1000)) {
if ($found) {
echo $line;
continue;
}
if (strpos($line, "ITEM DESCRIPTION:") !== FALSE) {
$found = true;
}
}
What about
$file = "file.txt";
$f = fopen($file, "r");
$start = false;
while ($line = fgets($f, 1000)) {
if ($start) echo $line;
if ($line == 'ITEM DESCRIPTION') $start = true;
}
?

Categories