function to read fixed length text

[email protected] Wed, 12 Mar 2003 17:42:51 +0100
Newsgroups php.dev
Message-ID <[email protected]>
Hi Devs,

as this is the first time I post to this list, I hope I do the right thing.
I programmed a nice little function, that is not very nitty but might 
be useful for other users as well.

See the script below.

Cheers!
-- 
{Bis bald
Frederic;}
____________

<?
// function to read a 'fixed lenght' text file in an array  
// delivers an array containg the values
//
// $string 		= string to read
// $lengths 	= array containing the lenghts of the input fields  
// $number 		= number of fields/columns
//
// Usage read_fl(string string, array lengths, int number)

function read_fl ($string, $lengths, $number)
	{
	// tests
	if (!is_array($lengths)) $err = "$lengths is not an array";
	$lang = count($lengths);
	if ($lang != $number) $err ="number of fields/columns and number of
length statements does not match";
	
		if (!$err) {
			$start = 0;
			$i = 0;
				foreach ($eingabelänge as $wert) 
					{
			   		$i++;
			   		$tmp[] = substr($zeile, $start,
$wert);
			   		$start = $start + $wert;
			   		}
			foreach ($tmp as $zahl) echo $zahl."<br>";
			}
	if ($err) return $err;
	else return $tmp;
	}
	
?>