total noob Posted January 6, 2012 Share Posted January 6, 2012 Hello to everyone Can someone help me on how to parse this CSV string? I do not know how to parse string using PHP.. #1# "4","+447980123456","+447781484145","","2009-07-08","10:38:15","hello "","" world","Orange" "5","+447980123456","+447781484146","","2009-07-08","10:38:55","hello world","Orange" This string can contain a much bigger data. I would like the result to be 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 Please guys if anyone of you knows how to parse this string it will be much appreciated. Im nearing my deadline for my work and this thing is keeping me from finishing it.. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/ Share on other sites More sharing options...
The Little Guy Posted January 6, 2012 Share Posted January 6, 2012 Try this: $str = '"4","+447980123456","+447781484145","","2009-07-08","10:38:15","hello "","" world","Orange" "5","+447980123456","+447781484146","","2009-07-08","10:38:55","hello world","Orange"'; $arr = str_getcsv($str); print_r($str); Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304929 Share on other sites More sharing options...
AyKay47 Posted January 6, 2012 Share Posted January 6, 2012 was going to post an example as well, little guy beat me to it.. for documentation on the function that you need, str_getcsv Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304930 Share on other sites More sharing options...
total noob Posted January 6, 2012 Author Share Posted January 6, 2012 Hello guys.. I get an error.. Fatal error: Call to undefined function str_getcsv() in C:\wamp\www\a.php on line 4 Sorry for this but im not really that expert on using php.. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304932 Share on other sites More sharing options...
AyKay47 Posted January 6, 2012 Share Posted January 6, 2012 what version of PHP are you running? Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304933 Share on other sites More sharing options...
total noob Posted January 6, 2012 Author Share Posted January 6, 2012 The version on of my php on my cpanel is 5.2 Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304935 Share on other sites More sharing options...
total noob Posted January 6, 2012 Author Share Posted January 6, 2012 Can you help me sir? thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304940 Share on other sites More sharing options...
AyKay47 Posted January 6, 2012 Share Posted January 6, 2012 The version on of my php on my cpanel is 5.2 then you do not have access to this function, however on the php manual page that I linked you to, I found a function that should do the trick, will need tweaked for your output needs. <?php function csv2array($input,$delimiter=',',$enclosure='"',$escape='\\'){ $fields=explode($enclosure.$delimiter.$enclosure,substr($input,1,-1)); foreach ($fields as $key=>$value) $fields[$key]=str_replace($escape.$enclosure,$enclosure,$value); return($fields); } function array2csv($input,$delimiter=',',$enclosure='"',$escape='\\'){ foreach ($input as $key=>$value) $input[$key]=str_replace($enclosure,$escape.$enclosure,$value); return $enclosure.implode($enclosure.$delimiter.$enclosure,$input).$enclosure; } $data=array("one","\"two\""); for ($i=0;$i<100;$i++) $data=array(array2csv($data),$i); echo "Encoded $i times...".var_export($data,true)."\n"; for ($j=0;$j<$i;$j++) $data=csv2array($data[0]); echo "Decoded $i times...".var_export($data,true)."\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304943 Share on other sites More sharing options...
total noob Posted January 6, 2012 Author Share Posted January 6, 2012 Thanks sir.. I really appreciate you effort on helping me.. I think i will modify the code on the link that you give me.. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/254498-php-string-parse-help/#findComment-1304947 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.