I need to start a new line every time it loops through and adds to the CSV document that is getting generated.
Code:
if(is_array($results_top_pages->getRows())){
$name = rand();
$myfile = fopen("bin/".$name.".csv", "w") or die("Unable to open file!");
foreach($results_top_pages->getRows() as $top_page){
$txt = array($top_page[0]. ",".$top_page[1]);
fputcsv($myfile, $txt);
}
fclose($myfile);
}
I have tried the following:
- array($top_page[0]. ",".$top_page[1]. "\n");
- array($top_page[0]. ",".$top_page[1]. "\r\n");
- array($top_page[0]. ",".$top_page[1]. "<br>");
As well as all the above with '' instead of ""
Still no luck. Everything that is generated is all one line when opened in excel.
Thanks in advance.
It should detect the line endings. From putcsv doc:
Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.
Hope this helps.
Lose the quoted comma in $txt = array($top_page[0]. ",".$top_page[1]); Do this instead:
$txt = array($top_page[0], $top_page[1]);
fputcsv automatically puts line endings. What you're actually doing in your example is sending a single element array.
Related
I have a text file with the following 2 lines
woof
woof99
I then have this code where it's supposed to take the username, example woof and take it out and replace it with nothing.
$file = fopen("../UsersProfile/".$MyUsername."/otherfiles/Following.txt","a") or die("Unable to open file");
$content = file_get_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt");
$newcontent = str_replace($Username."\n", '', "$content");
file_put_contents("../UsersProfile/".$MyUsername."/otherfiles/Following.txt", "$newcontent");
fclose($file);
the text file will then look like this with one line now.
woof99
the issue that I am having is that it isn't working for some reason. I believe it worked before but for some reason now it isn't. Is there an easier way to do this?
Thanks!
Edit: I found out what I needed to do I need to do \r\n
$newcontent = str_replace($Username."\n", '', $content);
Just turn this "$content" to this $content :D
I have a problem with a textfile.
I have a textfile with an empty line (always the last line is empty).
I need to remove this line. I tried several ways. Here is my current way to delete empty lines:
function RemoveEmptyLines($filename)
{
$myfile = fopen($filename, "r") or die("Unable to open file!");
$Content = fread($myfile,filesize($filename));
fclose($myfile);
$NewContent = preg_replace('/^\s+/m', '', $Content);
$myfile2 = fopen('new.txt', "w") or die("Unable to open file!");
fwrite($myfile2, $NewContent);
fclose($myfile2);
echo "removed";
}
This deletes all the empty lines within the textfile, but not the last empty line.
If the content is:
1 \n 2 \n \n 3
It deletes the empty line.
If it is:
1 \n 2 \n 3 \n \n
It doesn´t..
Any solutions?
The problem is, that the file is dynamic, so I can´t just delete the last line, because it´s not always empty..
Get the contents, trim the end and put the contents:
file_put_contents($filename, rtrim(file_get_contents($filename));
Or, for your existing code, you don't want m (PCRE_MULTILINE) and you want to match the end of the string $. I think this pattern will work:
/\s+$/
Both solutions will removes spaces as well. If you don't want that:
/[\n\r]+$/
If you really want to delete ALL empty lines then you can read the file into an array, ignoring empty lines and put the contents back:
file_put_contents($filename, file($filename, FILE_SKIP_EMPTY_LINES));
However, if you are creating this file you might want to look at that code and not add the newline in the first place.
Have you tried rtrim? Might be what you need:
rtrim($NewContent);
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);
}
?>
$size = intval(trim(fgets($fp,4)));
$triangle = range(1,$size);
for($j=0;$j<$size;$j=$j+1)
$triangle[$j] = split(" ",trim(fgets($fp,400)));
This code reads in the number of lines to read, then reads them one by one. Issue is, when first input line ends in space, it reads that space as a new line.
you can read full file content by file_get_contents.
<?php
$content= file_get_contents('myfile.txt');
echo $content;
?>
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.