Need help with parsing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kosaks
Forum Newbie
Posts: 10
Joined: Thu Aug 18, 2011 10:21 am

Need help with parsing

Post by kosaks »

Hello to everyone.

I have here my string
#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.",""
and my parsing code

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 />';
}
?>
My problem is that i need to drop the #1#, and split the string into an array on +447537404702.. However the result does not show my mobile numbers "+639217195804, +639105701066"

This is the result of the parsing code above
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.
)

)
What i would like to the result is also to add the mobile numbers. Please see below.
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.
)

)
Can someone help me modify this code so that i could get the result that i want? Any help will be appreciated...

thanks in advance
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Need help with parsing

Post by pickle »

I'm not completely clear what your end goal is. You say you want to split on "447537404702", but you're using that as your newline argument to your function.

If you truly are reading in a CSV, look into the fgetcsv() function - it does a lot/all of that parsing functionality for you.

http://ca3.php.net/manual/en/function.fgetcsv.php
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
kosaks
Forum Newbie
Posts: 10
Joined: Thu Aug 18, 2011 10:21 am

Re: Need help with parsing

Post by kosaks »

thanks sir...
Post Reply