Splitting array problem

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
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Splitting array problem

Post by mikebr »

I seem to be having a weird problem when extracting a text from a file. I have a file that contains text delimited with '::' and "|", this '::' is broken up and then the '|' sub string is broken down into it's various values. Everything goes well except for the odd string seems to get broken down yet again "on three of the entries (No pattern)", between the "|" delimiter is. There are 185 substrings in the file. For example: '36.3463889,6.6091667,0,1, ' would become '36.3463889,6.6091667' and ',0,1, ', anyone any ideas?

I have check the obvious.
For example the 'Illouz|35.7,-0.6166667,0,1, |1|1|134' string below seperates into '35.7,-0.6166667' and ',0,1, ' but the others are fine. 'Illouz' is actually the 84th '::' part of the string.

Code: Select all

$thestring = "Illouz|35.7,-0.6166667,0,1, |1|1|134::Isly|36.7833333,3.05,0,1, |1|1|134::Faubourg D'el Kantara|36.3694444,6.6211111,0,1, |1|1|134::";

$array = explode("::",$thestring);
$num2 = count($array); // Entry count
$array2 = array();

for ($i = 0 ; $i < $num2 ; $i++) {
			
$array2 = explode("|",$array[$i]);	// Explode the array to get the variables

$num = count($array2);


	for ($n = 0 ; $n < $num ; $n++) {
	$array3["arg".$n] = $array2[$n];
	}
	
	$location_name = str_replace("`","'",$array3['arg0']);
	$geo_data	= trim($array3['arg1']);
	$location_type	= trim($array3['arg2']);
	//$unused	= trim($array3['arg3']); //Unused
	$country_id	= trim($array3['arg4']);

//Check the values
echo "$country_id : $location_name : $geo_data : $location_type<br />";
}
Thanks
mikebr
Forum Contributor
Posts: 243
Joined: Sat Sep 28, 2002 7:05 am

Post by mikebr »

OK, think I got it...

$thestring = fgets($handle, 4096);

Removing the file length seems to sort the problem.
Post Reply