fopen write with variable issue - php

small project - stumped typing to get it working - if you have any ideas please let me know tku! This post program is requiring more words so here we go
<?php
$row = 1;
if (($handle = fopen("issue-heads-1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
if ($row == 2) {
$file = fopen($data[14], "w");
$write = '
<?php
include "/home/history/public_html/issue1.php";
echo \'<a class="prev" href="\' . $data[16] . \'">\';
?>
';
fwrite($file, $write);
fclose($file);
}
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>

Quick guess - Try (untested):
$write = '
<?php
include "/home/history/public_html/issue1.php";
echo \'<a class="prev" href="' . $data[16] . '">\';
?>
';
It's just a bit tricky with the multiple quotes... think you might have lost track of which ones need escaping...
Hmmmm... so that didn't work... the next thing I would try is to construct the $write variable over several lines (hopefully making the job a bit easier, so perhaps easier to avoid error) - note that I also threw in a repeating filewrite to see what the output is:
$hF = fopen('__debug.log', "a"); //outside your loop
//inside loop
$hrf = $data[16];
$write = '<?php' + "\n";
$write .= 'include "/home/history/public_html/issue1.php";' + "\n";
$write .= "echo '<a class=\"prev\" href=\"";
$write .= $hrf;
$write .= "\">';" + "\n";
$write .= '?>';
fwrite($hF, $write);
and make sure to close the file before your script ends:
//outside the loop
fclose($hF);

Using a variable inside a write statement didn't work while inside a fopen statement. I ended up having to use ob_start to get it to work. Hat tip to gibberish for getting me on the right path.
<?php
ob_start();
include 'issue1.php';
$issueone = ob_get_contents();
ob_end_clean();
$row = 1;
if (($handle = fopen("issue-heads-1.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle)) !== FALSE) {
$csv[] = $data;
}
fclose($handle);
}
$file = fopen($csv[$row][14], "w") or die("Unable to open file!");
fwrite($file, $issueone);
fwrite($file, "<a class=\"prev\" href=\"" . $csv[$row][16] . "\">");
fclose($file);
print "ok";
?>

If you have unique headers in your CSV
$headers = [];
while (false !== ($data = fgetcsv($handle))) {
if(empty($headers)){
$headers = $data; //["foo", "bar"] (for example)
continue; //skip to next iteration
}
//$data [1,2] (for example)
$row = array_combine($headers, $data);
//$row = ["foo"=>1,"bar"=>2] (for example)
}
Now you can use the text headers instead of 16 etc...
One thing to be careful of is array combine is very sensitive to the length of the arrays. That said if it errors you either have duplicate keys (array keys are unique) or you have either an extra , or a missing one for that line.
Array combine takes the first argument as the keys, and the second as the values and combines them into an associative array. This has the added benefit that the order of your CSV columns will not be important (which can be a big deal).
PS as I have no idea what your headers are, I will leave that part up to you. But lets say #16 is issues. Now you can simply do $row['issues'] just like a DB source etc...
Cheers.

Related

PHP: feof miss last word

the problem is simple but complicated at the same time.
feof doesn't print my last word. It take from file name city and code (Venice,A908) and should show in OUTPUT: nameCity,codeOfCity.
Let me show you an example:
City.csv
Abano Terme,A001
Abbadia Cerreto,A004
Abbadia Lariana,A005
Abbiategrasso,A010
Zubiena,M196
Zuccarello,M197
Zuclo,M198
Zungri,M204
Code:
<?php
$buffer = "";
$file = fopen("City.csv", "r");
//while (($c = fgetc($file)) != EOF )
//while (($c = fgetc($file)) != NULL )
//while (($c = fgetc($file)) !== false )
while(!feof($file))
{
$c = fgetc($file);
$buffer .= $c;
if($c == ",")
{
echo $buffer;
$buffer = "";
}
if($c == "\n")
{
echo $buffer."<br/>";
$buffer = "";
}
}
fclose($file);
?>
OUTPUT:
Abano Terme,A001
Abbadia Cerreto,A004
Abbadia Lariana,A005
Abbiategrasso,A010
Zubiena,M196
Zuccarello,M197
Zuclo,M198
Zungri,
Since it seems like you are just trying to output the file as is, with only change being to substitute HTML line breaks <br /> instead of new line characters why not simplify things?
echo nl2br(file_get_contents('City.csv'), true);
Or if you don't want to read the whole file into memory:
$file = fopen('City.csv', 'r');
while(!feof($file)) {
echo nl2br(fgets($file), true);
}
fclose($file);
In one of the comments above you mention that you want the city and city values available as variables (though your code example doesn't seem to indicate this). If that is the case, try fgetcsv() like this:
$file = fopen('City.csv', 'r');
while($values = fgetcsv($file)) {
$city = $values[0];
$city_code = $values[1];
echo $city . ',' . $city_code . '<br />';
}
fclose($file);
Your problem is, there's no newline at the end of your file, so it never hits the last "\n" check to output the buffer contents.
to fix this, you just need to put in another check on that conditional. change
if($c == "\n")
to:
if($c == "\n" || feof($file))
Here's a much cleaner and more concise version of your code if you'd like to use the correct function for parsing a csv file:
<?php
$buffer = array();
$file = fopen("City.csv", "r");
while(!feof($file) && $buffer[] = fgetcsv($file));
fclose($file);
foreach($buffer as $line){
echo join(',', $line).'<br/>';
}
?>

Use PHP to convert text file into array

I have a text file here which I need to be able to convert into rows to extract the second, third, fourth, and fifth values from.
The first 7 values of each row are tab delimited, then there is a newline, then the final three values are tab delimited.
I removed the interrupting newlines so that each row is fully tab delimited.
<?php
$file="140724.txt";
$fopen = fopen($file, "r");
$fread = fread($fopen,filesize("$file"));
fclose($fopen);
$remove = "\n";
split = explode($remove, $fread);
foreach ($split as $string)
{
echo "$string<br><br>";
}
?>
Which produces this.
I'm not sure where to progress from this point. I'm teaching myself PHP and am still quite new to it, so I don't even know if where I've started from is a good place. My instinct is to write the previous output to a new textfile, then create another block of code similar to the first but exploding based on tabs, this time.
Help?
You can process this file in one go like this:
<?php
$file="140724.txt";
$fopen = fopen($file, 'r');
$fread = fread($fopen,filesize($file));
fclose($fopen);
$remove = "\n";
$split = explode($remove, $fread);
$array[] = null;
$tab = "\t";
foreach ($split as $string)
{
$row = explode($tab, $string);
array_push($array,$row);
}
echo "<pre>";
print_r($array);
echo "</pre>";
?>
The result will be a jagged array:
You will need to clean up the 1st and the last element.
That is structured data, delimited by tabs. You can use fgetcsv() to read that data into an array. For an example see the PHP documentation.
<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
$text[] = fgets($myfile);
}
fclose($myfile);
print_r($text);
?>
There is another answer here which converts file/raw strings into an associative array. It is really very handy in such cases.
function tab_to_array($src='', $delimiter=',', $is_file = true)
{
if($is_file && (!file_exists($src) || !is_readable($src)))
return FALSE;
$header = NULL;
$data = array();
if($is_file){
if (($handle = fopen($src, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
fclose($handle);
}
}
else{
$strArr = explode("\n",$src);
foreach($strArr as $dataRow){
if($row = explode($delimiter,$dataRow))
{
if(!$header)
$header = $row;
else
$data[] = array_combine($header, $row);
}
}
}
return $data;
}
/**
* Example for file
*/
print_r(tab_to_array('example.csv'));
/**
* Example for raw string
*/
$str = "name number
Lorem 11
ipsum 22";
print_r(tab_to_array($str, "\t", false));

CSV not properly being parsed on new lines

I have a CSV file that looks like:
Header1,Header2,Header3
value1,value2,value3
value11,value12,value13
value21,value22,value23
etc...
When I use this code to parse through it:
$csvData = file_get_contents($file_url);
$lines = explode(" ", $csvData);
$array = array();
foreach ($lines as $line) {
$array[] = str_getcsv($line);
}
dpm($array);
the first value of a line always ends up being part of the end of the last header:
EDIT: There must have been an issue with cache as this is working fine.
For reading CSV file explode() and file_get_contents() are not the correct options. Go for fgetcsv
You can do like below for reading CSV file:
<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
?>
Use file instead of file_get_contents. That will return an array with one line of the input file on each index. This will work with all possible new line characters.
$lines = file($file_url, FILE_IGNORE_NEW_LINES)
You might want to break lines into newlines ("\n") instead of just spaces (" "). Even better, you might want to use file() to load file lines into an array. Better yet, you might want to use SplFileObject class to do that:
<?php
$file = new SplFileObject($file_url);
$file->setFlags(SplFileObject::READ_CSV | SplFileObject::SKIP_EMPTY
| SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD);
$data = array();
foreach ($file as $row) {
$data[] = $row;
}
Good luck!

PHP program will run and echo out nothing

I made a script that reads data from a .xls file and converts it into a .csv, then I have a script that takes the .csv and puts it in an array, and then I have a script with a foreach loop and at the end should echo out the end variable, but it echos out nothing, just a blank page. The file writes okay, and that's for sure, but I don't know if the script read the csv, because if I put an echo after it reads, it just returns blank.
Here my code:
<?php
ini_set('memory_limit', '300M');
$username = 'test';
function convert($in) {
require_once 'Excel/reader.php';
$excel = new Spreadsheet_Excel_Reader();
$excel->setOutputEncoding('CP1251');
$excel->read($in);
$x=1;
$sep = ",";
ob_start();
while($x<=$excel->sheets[0]['numRows']) {
$y=1;
$row="";
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
$y++;
}
echo $row."\n";
$x++;
}
return ob_get_contents();
ob_end_clean();
}
$csv = convert('usage.xls');
$file = $username . '.csv';
$fh = fopen($file, 'w') or die("Can't open the file");
$stringData = $csv;
fwrite($fh, $stringData);
fclose($fh);
$maxlinelength = 1000;
$fh = fopen($file);
$firstline = fgetcsv($fh, $maxlinelength);
$cols = count($firstline);
$row = 0;
$inventory = array();
while (($nextline = fgetcsv($fh, $maxlinelength)) !== FALSE )
{
for ( $i = 0; $i < $cols; ++$i )
{
$inventory[$firstline[$i]][$row] = $nextline[$i];
}
++$row;
}
fclose($fh);
$arr = $inventory['Category'];
$texts = 0;
$num2 = 0;
foreach($inventory['Category'] as $key => $value) {
$val = $value;
if (is_object($value)) { echo 'true'; }
if ($value == 'Messages ') {
$texts++;
}
}
echo 'You have used ' . $texts . ' text messages';
?>
Once you return. you cannot do anything else in the function:
return ob_get_contents();
ob_end_clean();//THIS NEVER HAPPENS
Therefore the ob what never flushed and won't have any output.
I see a lot of repetitive useless operations there. Why not simply build an array with the data you're pulling out of the Excel file? You can then write out that array with fputcsv(), instead of building the CSV string yourself.
You then write the csv out to a file, then read the file back in and process it back into an array. Which begs the question... why? You've already got the raw individual bits of data at the moment you read from the excel file, so why all the fancy-ish giftwrapping only to tear it all apart again?

How to change list to CSV, and vice versa, in PHP

I have a text file like....
arfc#net.xz
editor#magazine.com
nafi#cyber.net
nau#times.com
jai#mail.com
I want to convert it to CSV with the help of a little bit PHP, and I want to know also how it can be reversed...ie from CSV to an ordered, or un-ordered list.....kindly help me please :)
To convert to CSV
$data=file_get_contents("file");
$data =explode("\n\n",$data);
echo implode(",",array_filter($data));
Update as required to convert from CSV,
$data = explode(",", file_get_contents("file") );
echo implode("\n\n",$data);
For many rows of csv data, you can iterate the file using fgetcsv(). eg
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 2048, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
<?php
//read file
$content = file_get_content($filteredFilePath);
//explode contents into array
//if you use windows or mac, newlines may be different
//i.e: \r, \r\n
$list = explode("\n\n", $content);
//iterate over the items and print a HTML list
//to generate ordered list, use: <ol>
echo '<ul>';
foreach($list as $item) {
echo '<li>' . $item . '</li>';
}
echo '</ul>';
Edit: I made some modification to work with double newlines.

Categories