I need some help with this problem.
I'm work with laravel, and I got the select which look like this:
$table = Onlineszallas::all()->toArray();
I wanna write this query to a TXT file with latin2 file charset, every row in the table, in new line, strings separated by ; and every row end with;
I tried the fputcsv() method but if I open the txt file I found some string which contain space between to apostrophe like the name row.
"George Michael"
I need some help with file_put_contents() or any other method which can solve my problem!
Many thanks!
You can use Storage facade. Create a file:
use Illuminate\Support\Facades\Storage;
Storage::put('file.txt', $contents);
Add text content to a file:
Storage::append('file.txt', $someOtherContent);
But I would recomment you to store data as JSON:
$table = Onlineszallas::all()->toJson();
In this case you'll be able to easily import the data in any system.
Related
I am trying to generate a JSON string using php from an array, and I would like to store that string in a csv file, also with PHP. I want to do this, because I am working with a quite large amount of data, and I would like to use MySQL's LOAD DATA LOCAL INFILE to populate and update my database table.
This is the code I have:
$tmpFileProducts = 'path/to/file';
$tmpFileProductsHandler= fopen($tmpFileProducts, 'w');
foreach ($attributeBatch as $productId => $batch) {
fputcsv($tmpFileProductsHandler, array($productId, $batch['title'], $batch['parsed'], json_encode($batch['attributes'])), "|", "\"");
}
My problem is, that when I am creating the CSV file, the JSON double-quotes are not escaped, thus I end up with simmilar lines in my csv file:
43541|"telefon mobil 3l 2020 4g "|"2020-12-05 17:38:19"|"{""color"":""dark chrome"",""memory_value"":4294967296,""storage_value"":68719476736,""sim_slot"":""dual""}"
My first possible solution would be, to change the string enclosure of my CSV file, but what enclosure should I use, to ensure no conflicts could arrise with the inner json column? I am in complete control of the array that is stringified, it will only contain ASCII characters, in it.
Would there be a way, to keep the current string enclosure, and instead escape the JSON string somehow? Later on, I will need to fetch the data that is included to the database, and convert it to an array again.
DISCLAIMER: I am well aware that instead of storing the data as a JSON string, I could store it in a specific relational table (which I am also doing), but I would need quick access to this data, for a background script that is running, and I would like to save on the time of the queries, to the relational table, as when the background script will use this data, it doesn't need to search in it.
Follow up question: as I am explicitly telling the fputcsv function what to use as string enclosure shouldn't it automatically escape all the simmilar inner strings?
I would like to read a file, generally a text file, each record is starting with a with a specific code (filed name) in the line and ended by another specific code for a complete record. Each specific code is delimited by character ^ as its value in php into dump into sql database.
text file e.g.
001^UK2000009
008^S54/01/R/M/X,
009^Male
110^text1
200^text2
001^UK2000008
008^S54/012/R/M/X
009^Female
110^text1a
200^text2a
and so on...
This is similar to php constructor File_MARC
thanks in advance
First you have to read a file with file methods in php and than you can get a specific column name and it's value by below way
First read a single line from a file and than use a explode method to break that line into different elements with space delimitation.
$columns = explode(' ', $line_variable);
After generating columns I can see that each key values are delimited by ^ (cap) symbol so for that also we can use the explode method.
$newColumn =[];
foreach($columns as $column){
$splited = explode('^', $column);
$newColumn[][$splited[0]] = $splited[1];
}
print_r($newColumn);
This is just to give you an idea that how you can achieve your task but rest is completely dependent on you.
i'm trying to create a CSV using this solution:
Creating csv file with php
Something is wrong, i keep getting data stuffed into one cell like this:
each value should be in an own cell .. any ideas what's wrong?
seems like the csv uses the wrong delimiter.
thanks
I finally found the solution: seems like it doesn't work for the German version of Excel,
all I had to do is changing the delimiter to a semi-colon like this:
fputcsv($fp, $val, ";");
I have an application and I want to make the translation.
I want to externalise the translation and people who translate my application wants an Excel spreedsheet.
I have my messages in yaml format but I don't know how to extract them and make a csv or a spreedsheet.
The second step is that I want to extract the csv file to make a yaml file.
I have you got an idea?
Help please
I guess you are trying to convert the YAML files containing translations. This files are basically key values stored like this:
button.ok.value: Ok
button.ok.tooltip: Commits the action
Of course YAML can be more complicated, but if you have something like this, just replace the ':' char with ',' then save the file as CSV (or change the extension), open it using Excel. Then you can save it as xls format or whatever format you want.
If your file uses hierarchical nodes like:
button:
ok:
value: Ok
tooltip: Commits the action
Then you might want to write some script to iterate over the values (that's a tree traversal) and write the values to a file.
You should provide an example of how your YAML looks like since it's a very flexible format.
$csv = "out.csv";
$yaml = "in.yml";
fputcsv($csv, yaml_parse_file($yaml));
Requires http://pecl.php.net/package/yaml
I am trying to make a news feed type thing in php.
I have a text file - news.txt and a php file index.php.
I have done the surrounding code and opening/closing the text file. Now I am stuck how to insert the new news item $newsnew to the top of the news.txt file and how to delete the old bottom news file in the news.txt file.
Is there any way to do this without deleting the whole file and writing it all again?
EDIT: Each news item is just a small string, say 500 characters, a single line.
Use a database.
If you really must use text files, use a different file for every news-item and name them sequentially like:
news001.txt
news002.txt
etc.
Then you can just add and delete files, read the directory and display what´s there.
Use the file() function to import the items in news.txt as an array, and use array_unshift() to add the new first item, and array_pop() to remove the last item. Join the array back into a single string and write it to news.txt:
$items = file('news.txt');
array_unshift($items, 'New item 1');
array_pop($items);
$newstext = implode(PHP_EOL, $items);
// write $newstext to the external file
If this is a XML file you could read it, parse it and delete the last child in the DOM. But if you have all your data in a DB it could be much easier to rewrite the file every time.
EDIT: after your edit: yes, you can do it like this:
write your new line to a new file
read the old file line by line and write it to the new one
skip the last line (detected by counting or EOF)
delete the old file and rename the new
No, there is not. But you might consider storing the messages in revers order. That way you only need to append to news.txt when new news arrive.
You are not going to be able to prepend to the beginning of the file without writing the whole thing out again. You could append to the end of it with the "a" mode flag to fopen(), but still to delete the oldest item you'll need to write out the entire file again.
Really, a database is solution here instead of a single text file.
There are many ways you can do it using the flat text file, but I'm not really sure it it's worth it. You can use some lightweight structured file or embedded database. For example SQLite, which would store it in normal file, no additional setup needed.