1<?php 2/******************************** 3OSBib: 4A collection of PHP classes to manage bibliographic formatting for OS bibliography software 5using the OSBib standard. Taken from WIKINDX (http://wikindx.sourceforge.net). 6 7Released through http://bibliophile.sourceforge.net under the GPL licence. 8Do whatever you like with this -- some credit to the author(s) would be appreciated. 9 10If you make improvements, please consider contacting the administrators at bibliophile.sourceforge.net 11so that your improvements can be added to the release package. 12 13Mark Grimshaw 2005 14http://bibliophile.sourceforge.net 15********************************/ 16class PARSESTYLE 17{ 18 function PARSESTYLE() 19 { 20 } 21// parse input into array 22 function parseStringToArray($type, $subject, $map = FALSE) 23 { 24 if(!$subject) 25 return array(); 26 if($map) 27 $this->map = $map; 28 $search = join('|', $this->map->$type); 29 $subjectArray = split("\|", $subject); 30// Loop each field string 31 $index = 0; 32 $independentFound = FALSE; 33 foreach($subjectArray as $subject) 34 { 35 $dependentPre = $dependentPost = $dependentPreAlternative = 36 $dependentPostAlternative = $singular = $plural = FALSE; 37// First grab fieldNames from the input string. 38 preg_match("/(.*)(?<!`|[a-zA-Z])($search)(?!`|[a-zA-Z])(.*)/", $subject, $array); 39 if(empty($array)) 40 { 41 if($independentFound) 42 { 43 $independent['independent_' . ($index - 1)] = $subject; 44 $independentFound = FALSE; 45 } 46 else 47 { 48 $independent['independent_' . $index] = $subject; 49 $independentFound = TRUE; 50 } 51 continue; 52 } 53// At this stage, [2] is the fieldName, [1] is what comes before and [3] is what comes after. 54 $pre = $array[1]; 55 $fieldName = $array[2]; 56 $post = $array[3]; 57// Anything in $pre enclosed in '%' characters is only to be printed if the resource has something in the 58// previous field -- replace with unique string for later preg_replace(). 59 if(preg_match("/%(.*)%(.*)%|%(.*)%/U", $pre, $dependent)) 60 { 61// if sizeof == 4, we have simply %*% with the significant character in [3]. 62// if sizeof == 3, we have %*%*% with dependent in [1] and alternative in [2]. 63 $pre = str_replace($dependent[0], "__DEPENDENT_ON_PREVIOUS_FIELD__", $pre); 64 if(sizeof($dependent) == 4) 65 { 66 $dependentPre = $dependent[3]; 67 $dependentPreAlternative = ''; 68 } 69 else 70 { 71 $dependentPre = $dependent[1]; 72 $dependentPreAlternative = $dependent[2]; 73 } 74 } 75// Anything in $post enclosed in '%' characters is only to be printed if the resource has something in the 76// next field -- replace with unique string for later preg_replace(). 77 if(preg_match("/%(.*)%(.*)%|%(.*)%/U", $post, $dependent)) 78 { 79 $post = str_replace($dependent[0], "__DEPENDENT_ON_NEXT_FIELD__", $post); 80 if(sizeof($dependent) == 4) 81 { 82 $dependentPost = $dependent[3]; 83 $dependentPostAlternative = ''; 84 } 85 else 86 { 87 $dependentPost = $dependent[1]; 88 $dependentPostAlternative = $dependent[2]; 89 } 90 } 91// find singular/plural alternatives in $pre and $post and replace with unique string for later preg_replace(). 92 if(preg_match("/\^(.*)\^(.*)\^/U", $pre, $matchCarat)) 93 { 94 $pre = str_replace($matchCarat[0], "__SINGULAR_PLURAL__", $pre); 95 $singular = $matchCarat[1]; 96 $plural = $matchCarat[2]; 97 } 98 else if(preg_match("/\^(.*)\^(.*)\^/U", $post, $matchCarat)) 99 { 100 $post = str_replace($matchCarat[0], "__SINGULAR_PLURAL__", $post); 101 $singular = $matchCarat[1]; 102 $plural = $matchCarat[2]; 103 } 104// Now dump into $final[$fieldName] stripping any backticks 105 if($dependentPre) 106 $final[$fieldName]['dependentPre'] = $dependentPre; 107 else 108 $final[$fieldName]['dependentPre'] = ''; 109 if($dependentPost) 110 $final[$fieldName]['dependentPost'] = $dependentPost; 111 if($dependentPreAlternative) 112 $final[$fieldName]['dependentPreAlternative'] = $dependentPreAlternative; 113 else 114 $final[$fieldName]['dependentPreAlternative'] = ''; 115 if($dependentPostAlternative) 116 $final[$fieldName]['dependentPostAlternative'] = $dependentPostAlternative; 117 else 118 $final[$fieldName]['dependentPostAlternative'] = ''; 119 if($singular) 120 $final[$fieldName]['singular'] = $singular; 121 else 122 $final[$fieldName]['singular'] = ''; 123 if($plural) 124 $final[$fieldName]['plural'] = $plural; 125 else 126 $final[$fieldName]['plural'] = ''; 127 $final[$fieldName]['pre'] = str_replace('`', '', $pre); 128 $final[$fieldName]['post'] = str_replace('`', '', $post); 129 $index++; 130 $final[$fieldName]['pre'] = $pre; 131 $final[$fieldName]['post'] = $post; 132 } 133 if(!isset($final)) // presumably no field names... 134 $this->badInput($this->errors->text("inputError", "invalid"), $this->errorDisplay); 135// last element of odd number is actually ultimate punctuation 136 if(isset($independent) && sizeof($independent) % 2) 137 $final['ultimate'] = array_pop($independent); 138 if(isset($independent) && !empty($independent)) 139 $final['independent'] = $independent; 140 return $final; 141 } 142} 143?> 144