Parsing MARC Directory Info

I expected a record that looked like this:

LEADER 00000nas  2200000Ia 4500 
001    18971047 
008    890105c19079999mau u p       0uuua0eng   
010    07023955 /rev 
040    DLC|cAUG 
049    PSMM 
050    F41.5|b.A64 
090    F41.5|b.A64 
110 2  Appalachian Mountain Club 
245 14 The A.M.C. White Mountain guide :|ba guide to trails in 
       the mountains of New Hampshire and adjacent parts of Maine
246 13 AMC White Mountain guide 
246 13 White Mountain guide 
246 13 A.M.C. White Mountain guide 
260    Boston,|bThe Club, 
300    v. :|bill., maps (some fold., some col.) ;|c16 cm 
362 0  1st-     ed.; 1907- 
500    Title varies slightly 
651  0 White Mountains (N.H. and Me.)|xGuidebooks

but instead got a record that looked like this:

00939cas  2200265Ia 4500001001300000003000700013005001700020008004100037020001500078040001800093050001600111110003100127245012200158246003000280246002600310246003200336246003000368260005500398300005700453362003600510500002700546650001100573651007300584999001600657
ocm18971047
OCoLC
20020918102844.0
890105c19079999mau u p       0   a0eng 
  a0910146489
  aDLCcAUGdNHS
  aF41.5b.A64
2 aAppalachian Mountain Club.
14aThe A. M. C. White Mountain guide :ba guide to trails in the mountains of New Hampshire and adjacent parts of Maine.
13aAMC White Mountain guide.
13aWhite Mountain guide.
13aA.M.C. White Mountain guide
13aAMC White Mountain guide.
  aBoston, Mass. :bAppalachian Mountain Club,c1983.
  a550 p.bill., maps (some fold., some col.) ;c16 cm.
0 a1st- ed.; 1907- ; 25th ed. 1992
  aTitle varies slightly.
  aHiking
0aWhite Mountains (N.H. and Me.)xDescription and travelxGuide-books.
  aCL000018321

(some of the non-printable characters have been replaced with newlines for readability.)

After staring at that record for entirely too long, forgetting about it for a while, then returning again to think about how unreadable it was, then forgetting about it again, then taking one last look, I had that *duh* moment that made me realize what I should have seen on first glance: this is a MARC record that hasn’t had its directory parsed.

So here’s my short-but-handy-and-hopefully-usefull-to-somebody-sometime code to parse the directory and then the rest of the record. It assumes $records is an array of records.


foreach($records as $record){
	$temp = explode('', $record);
	$dir = $temp[0];
	$record = substr($record, (strlen($dir) + 1));

	$dir = substr($dir, 24);
	$dir_field = NULL;
	while($dir){
		$dir_field[] = substr($dir, 0, 12);
		$dir = substr($dir, 12);
	}
	
	$record = str_replace('', '|', $record);
	$marc = NULL;
	foreach($dir_field as $field){
		if(ereg_replace('[^0-9]', '', $field)){
			unset($temp);
			$len = substr($field, 3, 4);
			$pos = substr($field, 7, 5);
			$field = substr($field, 0, 3);
			$temp = substr($record, $pos, $len);
			if($field < 10)
				$temp = '  |'. $temp;
			$marc .= trim($field .'|'. $temp) .“\n”;
			$marc_array[$field] = $temp;
		}
	}
	echo $marc;
}

```</code>

The actual output of that code on that record is this:

001|  |ocm18971047
003|  |OCoLC
005|  |20020918102844.0
008|  |890105c19079999mau u p       0   a0eng
020|  |a0910146489
040|  |aDLC|cAUG|dNHS
050|  |aF41.5|b.A64
110|2 |aAppalachian Mountain Club.
245|14|aThe A. M. C. White Mountain guide :|ba guide to trails in the mountains of New Hampshire and adjacent parts of Maine.
246|13|aAMC White Mountain guide.
246|13|aWhite Mountain guide.
246|13|aA.M.C. White Mountain guide
246|13|aAMC White Mountain guide.
260|  |aBoston, Mass. :|bAppalachian Mountain Club,|c1983.
300|  |a550 p.|bill., maps (some fold., some col.) ;|c16 cm.
362|0 |a1st- ed.; 1907- ; 25th ed. 1992
500|  |aTitle varies slightly.
650|  |aHiking
651| 0|aWhite Mountains (N.H. and Me.)|xDescription and travel|xGuide-books.
999|  |aCL000018321

It includes a little bit of fudging that my other MARC parsing code demands, but works and is readable.