PHP fgetcsv - detecting too many records - php

I have the following csv file:
"Id","Title","Body","Tags"
"101","this title","
\"">.</>"";
","c# asp.net excel table"
which I want to convert into an array as follows:
Array
(
[0] => Array
(
[0] => Id
[1] => Title
[2] => Body
[3] => Tags
)
[1] => Array
(
[0] => 101
[1] => this title
[2] => \"">.</>"";
[3] => c# asp.net excel table
)
)
My code is:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$data[$c] = strip_tags($data[$c]);
}
$result[$row] = $data;
$row++;
}
fclose($handle);
return $result;
My problem is I am getting the following array:
Array
(
[0] => Array
(
[0] => Id
[1] => Title
[2] => Body
[3] => Tags
)
[1] => Array
(
[0] => 101
[1] => this title
[2] =>
\">.</>"";
)
[2] => Array
(
[0] => ,c# asp.net excel table"
)
)
In general, how do I avoid detecting too many recors when there is potentially code inside the fields (it's a StackOverflow data dump so some text fields have all kinds of programming code).

This string is not correctly escaped:
"
\"">.</>"";
"
All quote chars must have backslashes before them (or other escape char that you've passed into appropriate param.
And you should't pass 0 and comma to fgetcsv, they are already default: http://php.net/fgetcsv

Try opening the file using CSVed to make sure that it was properly formatted as CSV.
If the CSV is broken, then you can do some quick fix to the parsed result. For example:
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$data[$c] = strip_tags($data[$c]);
}
if (count($data) == 3) {
$data[1][2] .= $data[2].[0];
unset($data[2]);
}
$result[$row] = $data;
$row++;
}
fclose($handle);
return $result;

Related

How to separate csv array by column?

I have a giant list in excel, they are just two columns
name type
The file is currently being read:
$lines = array_map('str_getcsv', file('file.csv', FILE_IGNORE_NEW_LINES));
print_r($lines); returns:
name1;type
name2;type2
...
Array ( [0] => Array ( [0] => name1;type1) [1] => Array ( [0] => name2;type2)...
I would like to access separate name and type in an foreach
How can I do this?
Thanks
str_getcsv default delimiter is ',' so you need call it somehow with explicitly specifying ';' as delimeter
for example like this
$myGetCsv = function ($str) {
return str_getcsv($str, ';');
};
$lines = array_map($myGetCsv, file('file.csv', FILE_IGNORE_NEW_LINES));
Use this code for CSV file reading. it ease to use and understand how its work.
if (($handle = fopen('./suppression_invalid_emails.csv', "r")) !== false) {
//getting column name
$column_headers = fgetcsv($handle);
foreach ($column_headers as $header) {
//column as array;
$result[$header] = array();
}
// get data for column;
while (($data = fgetcsv($handle)) !== false) {
$i = 0;
foreach ($result as &$column) {
$column[] = $data[$i++];
}
}
fclose($handle);
echo '<pre>'; print_r($result); echo '</pre>';
}
i use two basic function for this 1st one is fopen for reading file and other is fgetcsv for getting data for column.
and result is:
Array
(
[reason] => Array
(
[0] => Mail domain mentioned in email address is unknown
[1] => Known bad domain
[2] => Known bad domain
)
[email] => Array
(
[0] => muneque#collegeclub.om
[1] => saharfa2000#hatmail.com
[2] => tawheeda81#yahoo.co
)
[created] => Array
(
[0] => 1502644294
[1] => 1502480171
[2] => 1502344320
)
)

PHP read Netlist from txt file

I have this file format of txt file generated from schematic software:
(
NETR5_2
R6,1
R5,2
)
(
NETR1_2
R4,2
R3,1
R3,2
R2,1
R2,2
R1,1
R1,2
)
I need to get this:
Array
(
[0] => Array
(
[0] => NETR5_2
[1] => R6,1
[2] => R5,2
)
[1] => Array
[0] => NETR1_2
[1] => R4,2
[2] => R3,1
[3] => R3,2
[4] => R2,1
[5] => R2,2
[6] => R1,1
[7] => R1,2
)
Here is code i try but i get all from input string:
$file = file('tangoLista.txt');
/* GET - num of lines */
$f = fopen('tangoLista.txt', 'rb');
$lines = 0;
while (!feof($f)) {
$lines += substr_count(fread($f, 8192), "\n");
}
fclose($f);
for ($i=0;$i<=$lines;$i++) {
/* RESISTORS - check */
if (strpos($file[$i-1], '(') !== false && strpos($file[$i], 'NETR') !== false) {
/* GET - id */
for($k=0;$k<=10;$k++) {
if (strpos($file[$i+$k], ')') !== false || empty($file[$i+$k])) {
} else {
$json .= $k.' => '.$file[$i+$k];
}
}
$resistors_netlist[] = array($json);
}
}
echo '<pre>';
print_r($resistors_netlist);
echo '</pre>';
I need to read between ( and ) and put into array values...i try using checking if line begins with ( and NETR and if yes put into array...but i don't know how to get number if items between ( and ) to get foreach loop to read values and put into array.
Where i im making mistake? Can code be shorter?
Try this approach:
<?php
$f = fopen('test.txt', 'rb');
$resistors_netlist = array();
$current_index = 0;
while (!feof($f)) {
$line = trim(fgets($f));
if (empty($line)) {
continue;
}
if (strpos($line, '(') !== false) {
$resistors_netlist[$current_index] = array();
continue;
}
if (strpos($line, ')') !== false) {
$current_index++;
continue;
}
array_push($resistors_netlist[$current_index], $line);
}
fclose($f);
print_r($resistors_netlist);
This gives me:
Array
(
[0] => Array
(
[0] => NETR5_2
[1] => R6,1
[2] => R5,2
)
[1] => Array
(
[0] => NETR1_2
[1] => R4,2
[2] => R3,1
[3] => R3,2
[4] => R2,1
[5] => R2,2
[6] => R1,1
[7] => R1,2
)
)
We start $current_index at 0. When we see a (, we create a new sub-array at $resistors_netlist[$current_index]. When we see a ), we increment $current_index by 1. For any other line, we just append it to the end of $resistors_netlist[$current_index].
Try this, using preg_match_all:
$text = '(
NETR5_2
R6,1
R5,2
)
(
NETR1_2
R4,2
R3,1
R3,2
R2,1
R2,2
R1,1
R1,2
)';
$chunks = explode(")(", preg_replace('/\)\W+\(/m', ')(', $text));
$result = array();
$pattern = '{([A-z0-9,]+)}';
foreach ($chunks as $row) {
preg_match_all($pattern, $row, $matches);
$result[] = $matches[1];
}
print_r($result);
3v4l.org demo
I'm not the king of regex, so you can find a better way.
The main problem are parenthesis: I don't know what are between closing and next open parenthesis ( )????( ), so first I replace every space, tab, cr or ln between, then I explode the string by )(.
I perform a foreach loop for every element of resulted array, matching every occurrence of A-z0-9, and add array of retrieved values to an empty array that, at end of foreach, will contain desired result.
Please note:
The main pattern is based on provided example: if the values contains other characters then A-z 0-9 , the regex fails.
Edit:
Replaced preliminar regex pattern with `/\)\W+\(/m`

Loop through a CSV file

How can i do a foreach with a CSV file.
Suppose i have fopen a csv file which looks like so ---
Array
(
[0] => Category ID
[1] => Category
[2] => Country
[3] => Original price
[4] => Reduce price
)
Array
(
[0] => 11
[1] => Phone cases
[2] => Denmark
[3] => 90,99
[4] => 120
)
Array
(
[0] => 13
[1] => Shoes
[2] => Denmark
[3] => 180,99
[4] => 200
)
So how can i loop through all the data's in store them in a array.
I have tried like so, but it does not work.
$all_data = array();
foreach ($result as $key => $obj) {
$doc = array();
$doc['id'] = $obj[$key];
$doc['category'] = $obj[$key];
$doc['country'] = $obj[$key];
$doc['original_price'] = $obj[$key];
$doc['reduce_price'] = $obj[$key];
// array push all documents into $all_data
$all_data [] = $doc;
}
Anyone knows how i can loop through those data!
Just using the PHP documentation :
if (($handle = fopen("yourfile.csv", "r")) !== FALSE) {
$all_data = array();
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($i==0){ continue; $i=1; }
// Remove the first iteration as it's not "real" datas
$all_data[] = array(
'id' => $data[0],
'category' => $data[1],
'country' => $data[2],
'original_price' => $data[3],
'reduce_price' => $data[4],
);
}
fclose($handle);
}
It must achieve what you want. It's always better using the built-in functions. If you can't use this one, let me know.
EDIT :
As #Blag said :
you can use to file_get_contents() and str_getcsv()
But this is a different approach that'll load the full file in a string (file_get_contents) and parse the string in the second time (str_getcsv).
This should give you an idea of how to proceed :
$objsOut=array();
foreach ($result as $key => $obj) {
$objOut = array(
'id'=>$obj[0]
);
$objsOut[]=$objOut;
}

How to insert array value in database table base on first array column names

I am importing data from csv file so I used that code
<?php
$path = "Export.csv";
$row = 1;
if (($handle = fopen($path, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$row++;
$data_entries[] = $data ;
}
fclose($handle);
}
echo"<pre>";
print_r($data_entries);
echo"</pre>";
?>
and my array output like that my first array showing column names after that all array showing value so i want to insert value base on first array column names
Array
(
[0] => Array
(
[0] => Type
[1] => PinCode
[2] => APN
[3] => County
)
[1] => Array
(
[0] => Auction
[1] => 503082537
[2] => 502-052-002
[3] => United States of America
)
[2] => Array
(
[0] => Auction
[1] => 21596378
[2] => 628-202-038
[3] => Australia
)
)
Try the below code :
//extract the column names
$fields = array_shift($data_entries);
$values = array();
// Append the values
foreach($data_entries as $value){
$values[]="('{$value[0]}', '{$value[1]}', '{$value[2]}', '{$value[3]}' )";
}
// Build the SQL
$sql = "INSERT INTO `TABLE_NAME` (" . implode(',' , $fields) . ") values " . implode(',', $values);
You might try a different approach, since your data is coming from a CSV file.
LOAD DATA LOCAL INFILE 'Export.csv' INTO TABLE YourTable;
There are many options, read the manual: http://dev.mysql.com/doc/refman/5.0/en/load-data.html

PHP, Building ID'd array from non-incremental ID'd CSV

Basically, here is my CSV File:
1,"Gold"
2,"English Version"
10,"Sword+0"
11,"Sword+1"
12,"Sword+2"
And so on, you get the idea. There are other parts where the ID is not incremental, perhaps one is 2899 and then the next one is 3020. I'm trying to build an array from this with fgetcsv();. I can do it fine, but I've failed so far to match up my array IDs with the ID from the CSV.
Here's a simple one that simply builds an incremental array from the file:
$file = fopen("item_proto.csv", "r");
$i = 1;
while(! feof($file)){
$gvar['item'][$i] = (fgetcsv($file));
$i++;
}
fclose($file);
This of course results in:
Array
(
[item] => Array
(
[1] => Array
(
[0] => 1
[1] => Gold
)
[2] => Array
(
[0] => 2
[1] => English Version
)
[3] => Array
(
[0] => 10
[1] => Sword+0
But I'd like [item][x] to match up with [item][x][y].
Try this:
$file = fopen("item_proto.csv", "r");
$i = 1;
while(! feof($file)){
$line = fgetcsv($file);
$gvar['item'][$line[0]] = $line;
$i++;
}
fclose($file);

Categories