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
Related
I want to know how to search for a string in a folder and display the line in php. I already have a snipset but it dont search in a folder but in ONE file i have tested by replacing /something/sisi.txt by /somthing/* and /something/*.txt .
The snipset:
$searchthis = "jean";
$matches = array();
$handle = #fopen("./something/sisi.txt", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
print_r($matches);
I tested scandir using PHP version 7.4 and it gave expected results.
The reason why I started the index of $i at 2 is because the first two indices refer to "." and ".." which wasn't necessary for the test.
edit: 11/7/2022 -> Additional optional Echo statements have been added to make the separation between files more clear.
The printr from the question code
More info on scandir is here:
https://www.php.net/manual/en/function.scandir.php
<?php
$dir = './something';
$files1 = scandir($dir);
$files2 = scandir($dir, 1);
//echo ("<p>Below will be the directory information...</p>");
$L = sizeof($files1);
//print_r($files1);
$results = array();
for($i = 2; $i < $L; $i++) {
//echo "<br>The value of i is: $i";
//echo "<br>The value of files1 at i is:".$files1[$i];
scanFile($files1[$i]);
}
function scanFile($filename) {
//echo("<p>Scanning the file: $filename</p>");
$searchthis = "jean";
$matches = array();
$handle = #fopen("./something/$filename", "r");
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
if(strpos($buffer, $searchthis) !== FALSE)
$matches[] = $buffer;
}
fclose($handle);
}
for ($i = 0; $i < count($matches); $i++) {
echo "$matches[$i] <br>";
}
}
?>
$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.
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 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;
}
?
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 );