Jump to content

Extracting data from CVS file


dcjones

Recommended Posts

Hi all,

 

I am working with some data in a CVS file. The content following the following format:

 

AEDBuy=6.4452&AEDSell=5.1002&ANGBuy=3.164&ANGSell=2.298&ARSBuy=6.8626&ARSSell=5.3583

 

So far I have managed to replave the & sign with a comma to make it a truve CVS but I am now trying to extract the data into an array which I can manipulate and feed it into a SQL tabel. I have also remove the = sign.

 

 

$file_handle = fopen("DATA/lgwrates.txt", "rb");

while (!feof($file_handle) ) {


$line_of_text = fgets($file_handle);

if($line_of_text


$line_of_text = str_replace("&", ",", $line_of_text);
$line_of_text = str_replace("=", "", $line_of_text);

$parts = explode(',', $line_of_text);


foreach( $parts as $key=> $value){
$rate = $value;
$country =  substr($rate,1,3);
$sellRate = substr($rate,6);

 

The issue I have is how can I read the data from the file so that I end up with:

 

AEDBuy6.4452AEDSell=5.1002,ANGBuy3.164ANGSell=2.298,ARSBuy6.8626ARSSell=5.3583 and so on.

 

I may be going about this completely wrong so if someone could point me in the right direction that would be great.

 

 

Many thanks in advance.

 

 

Dereck

 

Link to comment
https://forums.phpfreaks.com/topic/200183-extracting-data-from-cvs-file/
Share on other sites

just a thought...

<?PHP

##########################################
#  example presumes:
#			1. there is just one continous string
#			2. the buys and sells are in pairs
#			3. there are no pipes "|" or tildes "~" in the string
##########################################

################################################
# for example purposes, $string represents the file contents
################################################
$string = "AEDBuy=6.4452&AEDSell=5.1002&ANGBuy=3.164&ANGSell=2.298&ARSBuy=6.8626&ARSSell=5.3583";

###########################################
#	replace "Buy=" and "Sell=" with the tilde "~"
###########################################
$string = str_replace ("Buy=" , "~", $string);
$string = str_replace ("Sell=" , "~", $string);

#######################################################
# explode the string into an array  using the ampersand as delimiter
#######################################################
$ar_1 = explode("&", $string);

########################################################
#  count elements on the array
########################################################
$ce1 = count($ar_1);

##########################################################
# create a temporay empty array
##########################################################
$new_a = array();

##########################################################
# initialize a counter
##########################################################
$i=0;

while($i<$ce1) {
$x = $i + 1;
$el1 = $ar_1[$i] . "|" . $ar_1[$x];
$new_a[] = $el1;
$i = $i +2;
}
$ce2 = count($new_a);

#####################################
#  just so you can see what is happening
# you don't need this section in your actual code
####################################
echo "<PRE>";
print_r($new_a);
echo "</pre>";

####################################
# create an empty array
####################################
$new_x = array();

##########################################################
# initialize a counter
##########################################################
$i=0;

###########################################
#	each element of the array  $new_x
#	contains the id (AED, ANG etc) the buy price and
#	the sell price separated by commas
###############################################
while($i<$ce2) {
$tempjob = explode("|",$new_a[$i]);
$elx = explode("~", $tempjob[0]);
$id = $elx[0];
$buy = $elx[1];
$ely = explode("~", $tempjob[1]);
$sell = $ely[1];
$new_x[] = $id . "," . $buy . "," . $sell;
$i++;
}

#####################################
#  just so you can see what is happening
# you don't need this section in your actual code
####################################

echo "<PRE>";
print_r($new_x);
echo "</pre>";

?>

 

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.