PHP prevent extra empty line when writing text to file - php

I'm having a small issue regarding a foreach() loop and writing an array to a text file within.
The loop gives me the format in the file that I want but it also adds (as I was told) an unwanted empty line at the end of the file.
Here is my piece of code:
foreach($data_arr as $data => $input)
{ fwrite($fh, $data . ":" . $input . "\n") or die("something went wrong here"); }
Is there a way to prevent this from happening and not add the \n when it reaches the end of the array?

You can do something like this :
$data_array = ['c', 'h', "hf"];
$last = count($data_array) - 1; #size of the array
foreach($data_array as $data => $input)
{
$separator = $data == $last ? "" : "\n"; #if is last, then seperator isn't a back to line
fwrite($fh, $data . ":" . $input . $separator);
}

Just check if current index is the last one, then eventually add "\n" to the string
$last_index = count($data_arr)-1;
foreach ($data_arr as $data => $input)
{
$string = $data . ":" . $input;
if ($data != $last_index)
{
$string .= "\n";
}
fwrite($fh, $string) or die("something went wrong here");
}

Determining whether you are doing something for the last time, is cumbersome - for starters, you need to count your items, and then you need a loop index to compare to that count …
Much easier to determine, whether you are doing something for the first time. So just reverse your logic here - do not try to output “all lines, followed by a newline each, except the last one”, but instead, output “all lines preceded by a newline each, except the first one.”
You could use a simple boolean flag for that:
$is_first_line = true;
foreach($data_arr as $data => $input) {
fwrite($fh, ($is_first_line ? "" : "\n") . $data . ":" . $input);
$is_first_line = false;
}
Or you just append a variable before the line data all the time - and simply make that variable “empty” on the first iteration, and then fill it with a newline character for all the following ones:
$prefix = "";
foreach($data_arr as $data => $input) {
fwrite($fh, $prefix . $data . ":" . $input);
$prefix = "\n";
}

Related

How to put new line in php file fwrite

I am writing a file that is plan.php. It is a php file that I am writing. I am using \n to put the new line in that file but it is giving me save output.
Here is code:
$datetime = 'Monthly';
$expiry = '2017-08-07';
$expiryin = str_replace('ly', '',$datetime);
if($expiryin == 'Month' || $expiryin == 'Year') {
$time = '+ 1'.$expiryin.'s';
} else {
$time = '+'.$expiryin.'s';
}
$expiry_date = date('Y-m-d', strtotime($time, strtotime($expiry)));
$string = createConfigString($expiry_date);
$file = 'plan.php';
$handle = fopen($file, 'w') or die('Cannot open file: '.$file);
fwrite($handle, $string);
And the function is:
function createConfigString($dates){
global $globalval;
$str = '<?php \n\n';
$configarray = array('cust_code','Auto_Renew','admin_name');
foreach($configarray as $val){
$str .= '$data["'.$val.'"] = "'.$globalval[$val].'"; \n';
}
$str .= '\n';
return $str;
}
But it is giving the output like:
<?php .......something....\n.....\n
So my question is how to put the new line in this file.
Note: There is no error in code. I have minimized the code to put here.
As everyone already mentioned '\n' is just a two symbols string \n.
You either need a "\n" or php core constant PHP_EOL:
function createConfigString($dates){
global $globalval;
// change here
$str = '<?php ' . PHP_EOL . PHP_EOL;
$configarray = array('cust_code','Auto_Renew','admin_name');
foreach($configarray as $val){
// change here
$str .= '$data["'.$val.'"] = "'.$globalval[$val].'";' . PHP_EOL;
}
// change here
$str .= PHP_EOL;
return $str;
}
More info about interpreting special characters in strings http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
Replace ' with " ;-)
You can learn more about strings in PHP's manual: http://docs.php.net/manual/en/language.types.string.php
If you use \n inside a single-quoted string ($var = '\n';), it will be just that - the litteral string \n, and not a newline. For PHP to interpret that it should in fact be a newline, you need to use doublequotes ($var = "\n";).
$var = '\n'; // The litteral string \n
$var = "\n"; // Newline
PHP.net on double quoted strings
Live demo
\n does not work inside single quotes such as '\n'. You need to use double quotes "\n". So for your purpose, the change you need to make is:
function createConfigString($dates){
global $globalval;
$str = '<?php \n\n';
$configarray = array('cust_code','Auto_Renew','admin_name');
foreach($configarray as $val){
$str .= "$data['".$val."'] = '".$globalval[$val]."'; \n"; // change places of double and single quotes
}
$str .= "\n"; // change places of double and single quotes
return $str;
}

Loading CSV from remote file

I am writing the following code from a CSV to get stock data, When I have downloaded the string it is splitting it in the following way
<COMPANY NAME>,<STOCK PRICE>,<STOCK CHANGE>
<COMPANY2 NAME>,<STOCK PRICE2>,<STOCK CHANGE2>
I have tried to split the array by using the /n character using the PHP function explode. However this did not split it properly. Here is my code:
public function getQuotes()
{
$result = array();
$format = $this->format;
$stockString = "";
foreach ($this->stocks as $stock)
{
$stockString = $stockString . $stock . "+";
}
//Remove the last "+"
$stockString = substr($stockString,0,strlen($stockString)-1);
$s = file_get_contents("http://finance.yahoo.com/d/quotes.csv?s=". $stockString . "&f=" . $format . "&e=.csv");
//The splitting is to be done here.
}
}
Thank you in advance
Use file function instead of file_get_contents - it will split the content for you, as php manual says:
Returns the file in an array. Each element of the array corresponds to
a line in the file, with the newline still attached. Upon failure,
file() returns FALSE.
Then you can use str_getcsv for each element of array to get field values.
public function getQuotes()
{
$result = array();
$format = $this->format;
$stockString = "";
foreach ($this->stocks as $stock)
{
$stockString = $stockString . $stock . "+";
}
//Remove the last "+"
$stockString = substr($stockString,0,strlen($stockString)-1);
$s = file("http://finance.yahoo.com/d/quotes.csv?s=". $stockString . "&f=" . $format . "&e=.csv");
foreach($s as $line) {
$values = str_getcsv($line);
}
}

Remove quotation marks from fputcsv in PHP?

i have array in fputcsv and need output without quotation marks on cells with space. I tried remove with null char, but these are just additional spaces and its not usable solution for me. Any ideas please?
I have this code...
fputcsv($handle, array($zbozi['kod_zbozi'], $zbozi['nazev'] ),';');
and I receive...
123456;"hdd adata"
but I want...
123456;hdd adata
I created own function from current fputcsv.
<?php function vujo_fputcsv($handle, $fields, $delimiter = ',') { if (!is_resource($handle)) {user_error('fputcsv() první parametr musí být data, ale tys mě dal' . gettype($handle) . '!', E_USER_WARNING);
return false;}
$str = '';
foreach ($fields as $cell) {$str .= $cell . $delimiter;}
fputs($handle, substr($str, 0, -1) . "\n");
return strlen($str);} ?>

updating a file in specific part only in php

I need to update a file using php
Sample file:
#Start#
No. of records: 2
Name: My name,
Age: 18,
Date: 2013-07-11||
Name: 2nd name,
Age: 28,
Date: 2013-07-11||
#End#
I need to edit 'No. of records' on each time I add another record on file. And another record needs to be before '#End#'
I'm using
$Handle = fopen($File, 'a');
$data = .......
fwrite($Handle, $Data);
to add records
How can I edit 'No. of records' & add data before '#End#'?
Instead of modifying the file I would parse it, change the data in PHP an rewrite the file after that.
To achieve this, I would firstly create a function that parses the input into php arrays:
function parse($file) {
$records = array();
foreach(file($file) as $line) {
if(preg_match('~^Name: (.*),~', $line, $matches)) {
$record = array('name' => $matches[1]);
}
if(preg_match('~^Age: (.*),~', $line, $matches)) {
$record ['age'] = $matches[1];
}
if(preg_match('~^Date: (.*)\|\|~', $line, $matches)) {
$record ['date'] = $matches[1];
$records [] = $record;
}
}
return $records;
}
Secondly I would create a function that flattens the arrays back into the same file format again:
function flatten($records, $file) {
$str = '#Start#';
$str .= "\n\n";
$str .= 'No. of records: ' . count($records) . "\n\n";
foreach($records as $record) {
$str .= 'Name: ' . $record['name'] . ",\n";
$str .= 'Age: ' . $record['name'] . ",\n";
$str .= 'Date: ' . $record['name'] . "||\n\n";
}
file_put_contents($file, $str . '#End#');
}
Then use it like this:
$records = parse('your.file');
var_dump($records);
$records []= array(
'name' => 'hek2mgl',
'age' => '36',
'date' => '07/11/2013'
);
flatten($records, 'your.file');
In case if file is relatively small (easily fits in memory), you can use file() function. It will return array, which you can iterate, etc.
If the file is larger, you'll need to read it in the loop using fgets(), writing data to the new temporary file and replacing original file with it after you're done

Function for each subfolder in PHP

I am new in PHP and can't figure out how to do this:
$link = 'http://www.domainname.com/folder1/folder2/folder3/folder4';
$domain_and_slash = http://www.domainname.com . '/';
$address_without_site_url = str_replace($domain_and_slash, '', $link);
foreach ($folder_adress) {
// function here for example
echo $folder_adress;
}
I can't figure out how to get the $folder_adress.
In the case above I want the function to echo these four:
folder1
folder1/folder2
folder1/folder2/folder3
folder1/folder2/folder3/folder4
The $link will have different amount of subfolders...
This gets you there. Some things you might explore more: explode, parse_url, trim. Taking a look at the docs of there functions gets you a better understanding how to handle url's and how the code below works.
$link = 'http://www.domainname.com/folder1/folder2/folder3/folder4';
$parts = parse_url($link);
$pathParts = explode('/', trim($parts['path'], '/'));
$buffer = "";
foreach ($pathParts as $part) {
$buffer .= $part.'/';
echo $buffer . PHP_EOL;
}
/*
Output:
folder1/
folder1/folder2/
folder1/folder2/folder3/
folder1/folder2/folder3/folder4/
*/
You should have a look on explode() function
array explode ( string $delimiter , string $string [, int $limit ] )
Returns an array of strings, each of
which is a substring of string formed
by splitting it on boundaries formed
by the string delimiter.
Use / as the delimiter.
This is what you are looking for:
$link = 'http://www.domainname.com/folder1/folder2/folder3/folder4';
$domain_and_slash = 'http://www.domainname.com' . '/';
$address_without_site_url = str_replace($domain_and_slash, '', $link);
// this splits the string into an array
$address_without_site_url_array = explode('/', $address_without_site_url);
$folder_adress = '';
// now we loop through the array we have and append each item to the string $folder_adress
foreach ($address_without_site_url_array as $item) {
// function here for example
$folder_adress .= $item.'/';
echo $folder_adress;
}
Hope that helps.
Try this:
$parts = explode("/", "folder1/folder2/folder3/folder4");
$base = "";
for($i=0;$i<count($parts);$i++){
$base .= ($base ? "/" : "") . $parts[$i];
echo $base . "<br/>";
}
I would use preg_match() for regular expression method:
$m = preg_match('%http://([.+?])/([.+?])/([.+?])/([.+?])/([.+?])/?%',$link)
// $m[1]: domain.ext
// $m[2]: folder1
// $m[3]: folder2
// $m[4]: folder3
// $m[5]: folder4
1) List approach: use split to get an array of folders, then concatenate them in a loop.
2) String approach: use strpos with an offset parameter which changes from 0 to 1 + last position where a slash was found, then use substr to extract the part of the folder string.
EDIT:
<?php
$folders = 'folder1/folder2/folder3/folder4';
function fn($folder) {
echo $folder, "\n";
}
echo "\narray approach\n";
$folder_array = split('/', $folders);
foreach ($folder_array as $folder) {
if ($result != '')
$result .= '/';
$result .= $folder;
fn($result);
}
echo "\nstring approach\n";
$pos = 0;
while ($pos = strpos($folders, '/', $pos)) {
fn(substr($folders, 0, $pos++));
}
fn($folders);
?>
If I had time, I could do a cleaner job. But this works and gets across come ideas: http://codepad.org/ITJVCccT
Use parse_url, trim, explode, array_pop, and implode

Categories