Jump to content

Need help again from you guys


total noob

Recommended Posts

Hello to all...

 

Can someone help me on this one? I have a hard time on modifying this code. This code was given to me as the reply on my old post.. However i put the wrong string on that post.. Now my problem is how to parse again this 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.",""

I need to drop the #1#, and split the string into an array on +447537404702..Please refer on the code below

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#
"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.",""';

$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 is the wrong string

#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"

 

Can someone help me on this one? any help will be appreciated...

 

Thanks in advance!

 

This is the link on my old post http://www.phpfreaks.com/forums/index.php?topic=351398.new;topicseen#new

Link to comment
Share on other sites

Hello sir

 

I think im stuck on this part of the code

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;
		}
	}

When i change $result[1] into $result[2] i would get the result that i want however on that result it wont show my mobile numbers that is not equal +447537404702

0

test

2012-01-13

03:26:15

test another sample sms. this is a sample sms

1

test

2012-01-13

03:27:13

test sms sample 2. test only.

 

Thanks in advance sir....

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.