Jump to content

Need help on this one. . .


total noob

Recommended Posts

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

 

Link to comment
Share on other sites

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.

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.