Jump to content

Php string parse help


total noob

Recommended Posts

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

Link to comment
Share on other sites

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"; 
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.