Anyone share code in php to parse csv data . While am parsing data i got values inside double quotes as separate array so my logic will change . I need to store this data to mysql DB. My sample csv file is below
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice👍👍,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
code
$csv_file = 'csv/test.csv';
$file = fopen($csv_file, "r");
fgetcsv($file);
$count=0;
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$getData = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $getData);
$getData = implode(",", $getData);
$getData = '\''.substr($getData , 1, -1).'\'';
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
$package_name = trim($getData[0]);
$app_version_code = trim($getData[1]);
$app_version_name = trim($getData[2]);
$reviewer_language = trim($getData[3]);
ob_get_clean();
}
expected output
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice👍👍
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
I just ran this and got the output you indicated you were expecting:
<?php
$file = fopen('input.txt', "r");
while (($data = fgetcsv($file, 10000, ",")) !== FALSE) {
print_r($data);
}
Here's input.txt:
com,24,2.1.0.5,en,mido,2020-11-01T03:29:32Z,1604201372915,2020-11-01T03:29:32Z,1604201372915,5,,Nice👍👍,2020-11-01T06:34:23Z,1604212463397,"Hi Raju, Thank you so much",497230
com,24,2.1.0.5,en,athene_f,2020-11-01T04:19:52Z,1604204392095,2020-11-01T04:19:52Z,1604204392095,5,,So so,2020-11-01T06:33:58Z,1604212438170,"Hi dev, Thanks",497230
And here's the output:
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => mido
[5] => 2020-11-01T03:29:32Z
[6] => 1604201372915
[7] => 2020-11-01T03:29:32Z
[8] => 1604201372915
[9] => 5
[10] =>
[11] => Nice👍👍
[12] => 2020-11-01T06:34:23Z
[13] => 1604212463397
[14] => Hi Raju, Thank you so much
[15] => 497230
)
Array
(
[0] => com
[1] => 24
[2] => 2.1.0.5
[3] => en
[4] => athene_f
[5] => 2020-11-01T04:19:52Z
[6] => 1604204392095
[7] => 2020-11-01T04:19:52Z
[8] => 1604204392095
[9] => 5
[10] =>
[11] => So so
[12] => 2020-11-01T06:33:58Z
[13] => 1604212438170
[14] => Hi dev, Thanks
[15] => 497230
)
If you are not getting the expected output, one other thing to check is the file encoding on your CSV file. It could be that your PHP script is expecting your file to be utf-8 encoded, but your file is using another encoding. There are lots of resources online for detecting file encoding and converting using PHP.
These 2 lines are 100% incorrect...
$getData =explode(",", $getData);
$exp_array = explode(",", $getData);
givn, that $getData contains a string, after explode() it will be an array (reassigned to $getData).
On the next line, this array is tried to be exploded again, but this will fail and give you warnings or errors.
Turn up error reporting level and display all errors on screen during development.
I need to remove all elements that contains dot in array that i receive fron ftp_nlist(), this is how i do that:
$connection = ftp_connect($host);
if(!$connection){
echo "Can't connect to $host ($login:$password)\n";
#print_r(error_get_last());
ftp_close($connection);
exit();
}
ftp_login($connection, $login, $password);
ftp_pasv($connection, TRUE);
$files_list = ftp_nlist($connection, $path);
print_r($files_list);
for($i = 0; $i< count($files_list); $i++){
if( strstr($files_list[$i], '.') ){
unset($files_list[$i]);
}
}
print_r($files_list);
echo 'Ok';
However it works strange
Array
(
[0] => /httpdocs/favicon.ico
[1] => /httpdocs/mobile.html
[2] => /httpdocs/g
[3] => /httpdocs/index.html
[4] => /httpdocs/.
[5] => /httpdocs/member.html
[6] => /httpdocs/sitemap.xml
[7] => /httpdocs/animated_favicon1.gif
[8] => /httpdocs/rakuten.html
[9] => /httpdocs/y_key_8866d9fb86f18b30.html
[10] => /httpdocs/robots.txt
[11] => /httpdocs/..
[12] => /httpdocs/bbs.html
[13] => /httpdocs/version.php
[14] => /httpdocs/css
[15] => /httpdocs/nas
[16] => /httpdocs/googlee7e5921970ceb672.html
[17] => /httpdocs/about.html
[18] => /httpdocs/images
)
Array
(
[2] => /httpdocs/g
[10] => /httpdocs/robots.txt
[11] => /httpdocs/..
[12] => /httpdocs/bbs.html
[13] => /httpdocs/version.php
[14] => /httpdocs/css
[15] => /httpdocs/nas
[16] => /httpdocs/googlee7e5921970ceb672.html
[17] => /httpdocs/about.html
[18] => /httpdocs/images
)
Ok
As you can see not all elements with dots was removed. I can't see clear reason why.
Every time you unset() an array element the count() in the condition is decreased by 1. So instead of using count($files_list); in the condition (this is evaluated each iteration), set it first so it doesn't change:
$count = count($files_list);
for($i = 0; $i < $count; $i++){
if(strpos($files_list[$i], '.') !== false){
unset($files_list[$i]);
}
}
I would prefer foreach():
foreach($files_list as $key => $file){
if(strpos($file, '.') !== false){
unset($files_list[$key]);
}
}
Also notice that strpos() is better suited here.
What is the fastest way to reach a URL with PHP without any output needed?
I did a testing on the code below by using file_get_contents and CURL, but the result inconstant and very close to each other.
function abtest()
{
$arr_to_return = array();
$t = microtime(true);
file_get_contents('http://127.0.0.1/test.html');
$e = microtime(true);
$arr_to_return['file_get_contents'] = $e-$t;
$t = microtime(true);
$handle = curl_init('http://127.0.0.1/test.html');
curl_setopt($handle, CURLOPT_NOBODY, TRUE);
curl_exec($handle);
curl_close($handle);
$e = microtime(true);
$arr_to_return['curl'] = $e-$t;
return $arr_to_return;
}
$limit = 20;
$count = 0;
$result = array();
$filegetcontents = 0;
$curl = 0;
do{
$test = abtest();
$result['file_get_contents'][] = $test['file_get_contents'];
$filegetcontents += $test['file_get_contents'];
$result['curl'][] = $test['curl'];
$curl += $test['curl'];
$count++;
} while ( $count < $limit);
echo '<pre>';
print_r($result);
echo '</pre>';
echo 'Average: file_get_contents ('.($filegetcontents/$limit).') curl ('.($curl/$limit).')';
As per suggested, I put it into a function and loop 20 times on localhost with direct IP.
Result as below:
Array
(
[file_get_contents] => Array
(
[0] => 0.0055451393127441
[1] => 0.0056819915771484
[2] => 0.0056838989257812
[3] => 0.014945983886719
[4] => 0.014636993408203
[5] => 0.014842987060547
[6] => 0.0053179264068604
[7] => 0.015022039413452
[8] => 0.014861106872559
[9] => 0.015033960342407
[10] => 0.015429973602295
[11] => 0.015249013900757
[12] => 0.01471996307373
[13] => 0.015311002731323
[14] => 0.0054020881652832
[15] => 0.015032052993774
[16] => 0.0051870346069336
[17] => 0.014680147171021
[18] => 0.014776945114136
[19] => 0.015046119689941
)
[curl] => Array
(
[0] => 0.0013649463653564
[1] => 0.0017318725585938
[2] => 0.0013041496276855
[3] => 0.0013928413391113
[4] => 0.0013279914855957
[5] => 0.0013871192932129
[6] => 0.011061906814575
[7] => 0.0013771057128906
[8] => 0.010823011398315
[9] => 0.0015439987182617
[10] => 0.0015928745269775
[11] => 0.0014979839324951
[12] => 0.0016429424285889
[13] => 0.011864900588989
[14] => 0.0015189647674561
[15] => 0.0014710426330566
[16] => 0.0014939308166504
[17] => 0.001460075378418
[18] => 0.011038064956665
[19] => 0.010931015014648
)
)
Average: file_get_contents (0.012120318412781) curl (0.0038913369178772)
Seems result of CURL is better than file_get_contents most of the time. Any faster way to do this other than above?
i create a method that will read a file with the application/octet type and here are some of the code.
Raw data :
GTHHS;MEKID Interface;5496;2012-07-20;
NM1;081;IN1;980898989;2001-01-15;Mr;Gaf;Uhkil;Uhkil,Gaf;PRI;Gaf
$contents = file_get_contents($tmp_filename);
$stringContents = explode(";", $contents);
Now it gives me this output :
Array
(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
NM1
[4] => 081
[5] => IN1
[6] => 980898989
[7] => 2001-01-15
[8] => Mr
[9] => Gaf
[10] => Uhkil
[11] => Uhkil,Gaf
[12] => PRI
[13] => Gaf
PR1
[14] => 081
[15] => IN1
[16] => 20730089
[17] => 7 The Schooner
[18] => Auhaas
[19] => Huuula Ave
[20] =>
[21] => Kishma
PR2
[22] => 081
[23] => IN1
[24] => 232323233
[25] => 400006
[26] => HGD
[27] => M
[28] => M
[29] => 2007-10-16
[30] => 1976-03-31
);
How can i make the NM1, PR1 as the head of array like this :
Array (
[NM1] = array(
[0] => GTHHS
[1] => MEKID Interface
[2] => 5496
[3] => 2012-07-20
)
);
I am planning also to make the inner array [0]-[3] as json.
If you explode the contents by \n you have each line starting with that identifier. If you then just explode by ; in that line and add it as a sub array, you got it like you want.
This actually looks like a plain old CSV file with your ifentifier in line one. If so, try something like this:
$data = array();
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, ";", "\"", "\n")) !== FALSE)
{
$key = array_shift($row);
$data[$key] = $row;
}
fclose($handle);
}
echo json_encode($data);
http://php.net/manual/en/function.str-getcsv.php
I have an array like this:
Array
(
[0] => Accounts.csv
[1] => Book1.xlsx
[2] => Documents/
[3] => Documents/AdbWinApi.dll
[4] => Documents/AdbWinUsbApi.dll
[5] => Documents/adb.exe
[6] => Documents/boot.img
[7] => Documents/fastboot.exe
[8] => Documents/side_banner.jpg
[9] => Documents/source.properties
[10] => File Organization.docx
[11] => How to Manage Risks.docx
[12] => How to Trade Forex.docx
[13] => Ken Doc - Galad Letter Head.pdf
[14] => Ken Header.png
[15] => MX_2004_fwmx_2004_en.exe
[16] => xx.docx
[17] => asmack-master.zip
[18] => ca5rmhx7l4-Human Heart 2.7z
[19] => evasi0n7.exe
[20] => ken header.pdf
[21] => sp42471.exe
)
and I would like it to return an array like below;
Array
(
[0] => Accounts.csv
[1] => Book1.xlsx
[2] => Documents
[3] => File Organization.docx
[4] => How to Manage Risks.docx
[5] => How to Trade Forex.docx
[6] => Ken Doc - Galad Letter Head.pdf
[7] => Ken Header.png
[8] => MX_2004_fwmx_2004_en.exe
[9] => xx.docx
[10] => asmack-master.zip
[11] => ca5rmhx7l4-Human Heart 2.7z
[12] => evasi0n7.exe
[13] => ken header.pdf
[14] => sp42471.exe
)
I am using the code below;
{
private function listFiles($bucket = null , $prefix = null) {
$ls = S3::getBucket($bucket, $prefix);
if(!empty($ls)) {
foreach($ls as $l) {
$fname = str_replace($prefix,"",$l['name']);
if(!empty($fname)) {
$rv[] = $fname;
}
}
}
if(!empty($rv)) {
return $rv;
}
}
}
What changes can I make to my code to get the above results. I am using Donovan Schönknecht Amazon S3 library for codeigniter.
You could use a foreach
$newarr=array();
foreach($entries as $v)
{
if(strpos($v,'/')!==false)
{
$v=explode('/',$v);
if(isset($v[1]) && strlen($v[1])>0){}else{$newarr[]=$v[0];}
}
else { $newarr[]=$v;}
}
print_r($newarr);
Working Demo
You can overwrite the elements with $array[3] = ""; e.g. Then u have empty array-elements and then you could use a function like this:
function empty_array($array){
foreach($array as $ar => $value) {
if($value == '') {
unset($array[$ar]);
}
}
return $array;
}
$new_array = emtpy_array($old_array);