How to tell explode to ignore line feeds? - php

I have written some small code to read in a flat text file and display it. My issue is that one of the fields has alot of text in it and also some line returns. My code is using the line return is a delimiter and moving onto the next record.
How can I tell it to only split by the delimiter and ignore line returns?
Sample code I am using:
$delimiter = chr(1);
$fp = fopen('app2','r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
$loop = 0;
while (!feof($fp)) {
$loop++;
$line = fgets($fp,2048); //use 2048 if very long lines
$field[$loop] = explode ($delimiter, $line);
echo '
<tr>
<td>'.$field[$loop][0].'</td>
<td>'.$field[$loop][1].'</td>
<td>'.$field[$loop][2].'</td>
<td>'.$field[$loop][3].'</td>
<td>'.$field[$loop][4].'</td>
<td>'.$field[$loop][5].'</td>
<td>'.$field[$loop][6].'</td>
<td>'.$field[$loop][7].'</td>
<td>'.$field[$loop][8].'</td>
<td>'.$field[$loop][9].'</td>
<td>'.$field[$loop][10].'</td>
<td>'.$field[$loop][11].'</td>
<td>'.$field[$loop][12].'</td>
<td>'.$field[$loop][13].'</td>
<td>'.$field[$loop][14].'</td>
<td>'.$field[$loop][15].'</td>
<td>'.$field[$loop][16].'</td>
</tr>';
$fp++;
}
fclose($fp);
?>
You can see what it is doing here http://www.smartphonesoft.com/fred/xmlfeed/test/itunes_to_mysql.php

You can paste your textfile into one $var, by jaust adding all with .= , as long as your file is not 2GB large.
Note: string can be as large as 2GB.

Related

simple date visitor counter counting the text lines

i'm trying to make visitors count without sql with simple php code
counting the date list
i have log file called visits.log contains
2018-06-28
2018-06-28
only two lines
when i try to echo the lines gives me 5 i donno how but when i try to replace the log with something like
test
test
gives me 2 visitors
note: the text file doesn't contain empty space lines.
my php code
<p class="">Views today</p>
<span class=""><?php
$file="../../visitors.log";
$linecount = 0;
$handle = fopen($file, "r");
while(!feof($handle)){
$line = fgets($handle);
$linecount++;
}
fclose($handle);
echo $linecount-1;
?></span>
my adding lines code
$line = date('Y-m-d') . " - $_SERVER[REMOTE_ADDR]";
file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND);
Try the below code to get number of lines
$no_of_lines = count(file($file));
thanks for help guys my script was adding a hidden empty lines and i deal with it with replace()
replacing the empty lines to remove it

Removing spaces from a text file using PHP

I need to add itens from a text file, into a MySQL database, to do that i'm trying to use PHP to read the file and insert everything into the database, the problem is that the text file, has 2 itens per line, divided by spaces, something like this:
teste.txt
ITEM1 ITEM2
ITEM323 ITEM4
ITEM54 ITEM6
ITEM34234 ITEM8
I'm trying to remove the spaces using explode, but because the number of spaces, is random, i cannot do that. This is my Code:
$handle = #fopen("teste.txt", "r"); //read line one by one
while (!feof($handle)) // Loop til end of file.
{
$buffer = fgets($handle, 4096); // Read a line.
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
//values.=($a,$b);// save values and use insert query at last or
echo $a;
echo "<br>";
echo $b . "<br>"; // NEVER echoes anything
}
What should i do?
You can insert a method to change multiple spaces to one:
$buffer = preg_replace('/\s+/',' ',$buffer);
Right before exploding it:
$buffer = fgets($handle, 4096); // Read a line.
$buffer = preg_replace('/\s+/',' ',$buffer);
list($a,$b)=explode(" ",$buffer);//Separate string by Spaces
You can use preg_replace() to perform a regular expression:
$buffer = preg_replace("/\ +/", " ", fgets($handle, 4096));

PHP: Opening .txt file, reading line by line, removing (YEAR)

I am looking for a suggestion on this:
I have a text file called movies.txt with about 900 lines, which contains one movie name per line. However, I would like to remove the year the movie has been released using PHP (which I am new to)
The format is basically:
A Nous la Liberte (1932)
About Schmidt (2002)
Absence of Malice (1981)
Adam's Rib (1949)
Adaptation (2002)
The Adjuster (1991)
The Adventures of Robin Hood (1938)
Affliction (1998)
The African Queen (1952)
So I am looking for a way to open the text file, reading it line by line and removing the (YEAR) values while also removing the space before the (YEAR).
Then I would like to save it as newmovies.txt
Would be great if you could show and explain me a solution that works for my needs. I am still very new to PHP (started a week ago) so it's all still magic to me.
You can read a file line-wise using the file() function. Then foreach over that and strip lines until the opening parenthesis.
For example
foreach (file($fn) as $line) {
$output[] = strtok($line, "(");
}
You may need to trim the extra space and add linebreaks again.
So a regex might be simpler and also asserts some structure without blindly cutting things off:
$text = file_get_contents($fn);
$text = preg_replace('/\s*\(\d+\)/m', '', $text);
# \s* is for spaces and \d+ is a placeholder for numbers
Then save that back.
<?php
$toWrite = "";
$handle = #fopen("input.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$toWrite .= preg_replace('/\(\d+\)/', '', $buffer) . "\n";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
file_put_contents("input.txt", $toWrite);
}
?>

PHP: fgets equivalent for reading from the _bottom_ of a file?

I have a file that updates itself by appending, however this file becomes huge (500MB). I would like to read the last 50 lines in the file. How can this be done?
Tail -n50 will return the last 50 lines of the file.
$filename = 'test.html';
$output = shell_exec('exec tail -n50 ' . $filename);
echo $output;
Therefore, you don't have to load the entire file in memory.
Edit:
If you want to echo "<br>" after each line you do:
echo str_replace(PHP_EOL, '<br />', $output);
You'll need to use fseek to move the file pointer a certain number of bytes from the end of the file:
$fp = fopen('myfile','r');
fseek($fp,-1024, SEEK_END);
$last_kb_of_file = fgets($fp,1024);
You'll have to tell fgets how many bytes you want to read, not how many lines. It has no idea what the format of the file is. You'll have to split the result on a newline and see if you have 50 lines.

In PHP when using fgets() to read from file how to exclude "brake row"

I'm writing simple function witch will read data from myfile.txt with fgets().
Content of file is something like:
1
2
3
4
Function to get first value (1):
$f = fopen ("myfile.txt", "r");
$mystring = fgets ($f);
Now, when I use $mystring to write in file like:
$f1 = fopen ("myfile2.txt", "w");
fwrite($f1, 'text' . $mystrin . 'more text');
text 'more text' goes to new row.
I want to have it in in same row with other text.
When reading a file using fgets() it will include the newline at the end. You only need to remove it.
$mystring = trim ($mystring);
This will remove any leading and trailing whitespaces.
$mystring = rtrim ($mystring, PHP_EOL);
Will remove any trailing newlines.

Categories