I have here my string
and my parsing code#1#
"3032117","+639217195804","+447537404702","test","2012-01-13","03:26:15","test another sample sms. this is a sample sms",""
"3032118","+639105701066","+447537404702","test","2012-01-14","03:27:12","test sms sample 2. test only.",""
Code: Select all
<?php
function parse_csv($line,$delimiter=',',$quote = '"',$newline="\n") {
$line = str_replace("\r\n",$newline,$line); //change extended newlines.
$line = str_replace("\n",$newline,$line); //this is here in case you pass a different newline to the function.
$line = str_replace('"",""','',$line); //remove double quoted commas.
$line = str_replace($quote,'',$line); //remove quotes.
$line = preg_replace('~,{2,}~',',',$line); //remove double commas.
if(strstr($line,$newline)) {
$parts = explode($newline,$line); //all new lines should be an array.
}
if(isset($parts) && is_array($parts)) { //if a newline exists
foreach($parts as $value) { //rerun the function on each line.
$result = parse_csv($value,$delimiter,$quote,$newline);
//only include the results, if there is more than 1 line;
if(isset($result[2])) {
$arr[] = $result;
}
}
}
else {
$arr = explode($delimiter,$line); //make an array based on the delimiter.
//cleanup = delete lines that has no values.
foreach($arr as $k => $v) {
if(empty($v)) {
unset($arr[$k]);
}
}
}
return $arr;
}
$file ='#1#
"3032117","+639217195804","+447537404702","test","2012-01-13","03:26:15","test another sample sms. this is a sample sms",""
"3032118","+639105701066","+447537404702","test","2012-01-13","03:27:13","test sms sample 2. test only.",""';
$array = parse_csv($file,',','"',"+447537404702");
echo '<pre>' . print_r($array,true) . '</pre>'; //show the array construct.
//echoing the values out.
foreach($array as $key => $value) {
echo '<h4>' . $key . '</h4>';
foreach($value as $v2) {
echo $v2 . '<br />';
}
echo '<hr />';
}
?>
This is the result of the parsing code above
What i would like to the result is also to add the mobile numbers. Please see below.Array
(
[0] => Array
(
[1] => test
[2] => 2012-01-13
[3] => 03:26:15
[4] => test another sample sms. this is a sample sms
)
[1] => Array
(
[1] => test
[2] => 2012-01-13
[3] => 03:27:13
[4] => test sms sample 2. test only.
)
)
Can someone help me modify this code so that i could get the result that i want? Any help will be appreciated...Array
(
[0] => Array
(
[1] => +639217195804
[2] => test
[3] => 2012-01-13
[4] => 03:26:15
[5] => test another sample sms. this is a sample sms
)
[1] => Array
(
[1] => +639105701066
[2] => test
[3] => 2012-01-13
[4] => 03:27:13
[5] => test sms sample 2. test only.
)
)
thanks in advance