Php Save fopen Result To A String - php

How can I get the "$buffer" value into a string, and use it outside the fopen and fclose functions? Thanks.
$handle = #fopen("http://www.example.com/", "r");
if ($handle) {
while (!feof($handle)) {
$buffer = fgetss($handle, 5000);
echo $buffer ;
}
fclose($handle);
}

Try file_get_contents() :
$buffer = file_get_contents("/my/file.txt");

$handle = #fopen("http://www.example.com/", "r");
$buffer = '';
if ($handle) {
while (!feof($handle)) {
$buffer .= fgetss($handle, 5000);
}
fclose($handle);
}

The easiest way is to use file_get_contents:
$buffer = file_get_contents("http://www.exemple.com");

$buffer is itself a string. instead of printing it using echo just concatenate it there and print it or use it with all together after the loop ends.
$buffer = '';
if ($handle) {
while (!feof($handle)) {
$buffer .= fgetss($handle, 5000);
}
fclose($handle);
}
//print the whole stuff:
echo $buffer;
And if you want to get all the stuffs only no other processing try using:
file_get_contents

$handle = #fopen("http://www.example.com/", "r");
$buffers = array();
if ($handle) {
while (!feof($handle)) {
$buffers[] = fgetss($handle, 5000);
}
fclose($handle);
}
print_r ( $buffers );

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.

Function for opening file

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;

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

read javascript file with PHP

please help me
I would like to read a javascript file and retrieve only lines starting with url.instance = "www.google.fr"; in input
in order to be able to modify these url
$handle = fopen('javascriptfile.js', 'r');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
//echo $buffer."<br>";
echo "<input type='text' value='$buffer'>";
}
fclose($handle);
}
I have write this code but I don't know how to continue please help me
You can search each line with stripos
<?php
$handle = fopen('javascriptfile.js', 'r');
$match = 'url.instance = "www.google.fr";';
if ($handle) {
while (!feof($handle)) {
$buffer = fgets($handle);
// check each line start with : url.instance = "www.google.fr";
if (stripos($buffer, $match) === 0 ) {
echo "<input type='text' value='$buffer'>";
}
}
fclose($handle);
}

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