total noob Posted January 9, 2012 Share Posted January 9, 2012 Hello again to all.. I have trouble of modifying this code.. A person gave me the link on where can i find answers to my problem on PHP String Parsing.. However it took me the whole weekend to modify the code.. I really have no idea on how to get the results that i want.. Could someone help me? This is the code <?php function parse_csv($file,$number,$comma=',',$quote='"',$newline="\n") { $db_quote = $quote . $quote; $file = trim($file); $file = str_replace("\r\n",$newline,$file); $file = str_replace($db_quote,'"',$file); $file = str_replace('#1#','',$file); $file = str_replace($number,'',$file); $file = str_replace($s++,'',$file); $file = str_replace(',",',',,',$file); $file .= $comma; $inquotes = false; $start_point = 0; $row = 0; for($i=0; $i<strlen($file); $i++) { $char = $file[$i]; if ($char == $quote) { if ($inquotes) { $inquotes = false; } else { $inquotes = true; } } if (($char == $comma or $char == $newline) and !$inquotes) { $cell = substr($file,$start_point,$i-$start_point); $cell = str_replace($quote,'',$cell); $cell = str_replace('"',$quote,$cell); $data[$row][] = $cell; $start_point = $i + 1; if ($char == $newline) { $row ++; } } } return $data; } $file ='#1# "4","+447980123456","+447781484145","","2009-06-08","10:38:15","hello "","" world","Orange" "5","+447980123456","+447781484146","","2009-07-08","10:38:55","hello world","Orange"'; $array = parse_csv($file,"+447980123456"); print_r($array); ?> This is the result of that code Array ( [0] => Array ( [0] => 4 [1] => [2] => +447781484145 [3] => [4] => 2009-06-08 [5] => 10:38:15 [6] => hello "," world [7] => Orange 5 [8] => [9] => +447781484146 [10] => [11] => 2009-07-08 [12] => 10:38:55 [13] => hello world [14] => Orange ) ) What i would like as the result is a string that shows this information.. Originator : +447781484145, Date : 2009-07-07, Time : 10:38:15, Message : hello world Originator : +447781484146, Date : 2009-07-08, Time : 10:38:55, Message : hello world Any help will be appreciated.. thanks in advance.. Quote Link to comment https://forums.phpfreaks.com/topic/254646-need-help-on-this-one/ Share on other sites More sharing options...
Muddy_Funster Posted January 9, 2012 Share Posted January 9, 2012 what happens if you stick this at the end? foreach($array as $arr){ echo "<br>"; foreach ($arr as $set => $value){ echo "$value "; } } Quote Link to comment https://forums.phpfreaks.com/topic/254646-need-help-on-this-one/#findComment-1305758 Share on other sites More sharing options...
jcbones Posted January 9, 2012 Share Posted January 9, 2012 So, you need to drop the #1#, and split the string into an array on +447980123456? <?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[1])) { $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# "4","+447980123456","+447781484145","","2009-06-08","10:38:15","hello "","" world","Orange" "5","+447980123456","+447781484146","","2009-07-08","10:38:55","hello world","Orange"'; $array = parse_csv($file,',','"',"+447980123456"); 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 function would put each line in a separate array. Where as your current function puts it all in one array, which is much harder to separate out. commented up, hope it helps you along the way. Quote Link to comment https://forums.phpfreaks.com/topic/254646-need-help-on-this-one/#findComment-1305763 Share on other sites More sharing options...
total noob Posted January 9, 2012 Author Share Posted January 9, 2012 Wow... this is great! this is really what i need in order to finish my work... thank you so much for your help guys i really appreciate it.. God Bless! Quote Link to comment https://forums.phpfreaks.com/topic/254646-need-help-on-this-one/#findComment-1305784 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.