/** * Format the author names (APA) * * @param $authors * * @return string */ function formatAuthors($authors) { $authors = (array) $authors; //Count the number of contributors in the array $count = count($authors); //Count the number of authors in the array $numAuthors = 0; foreach ($authors as $contributor) { if (Utility::array_get($contributor, 'cselect') == 'author') { $numAuthors++; } } $ret = ''; for ($i = 0; $i < $count; $i++) { //If this contributor is an author if (Utility::array_get(Utility::array_get($authors, $i, []), 'cselect') == 'author') { if ($i == 0) { //First time through the loop if ($numAuthors > 1) { //There is more than one author $ret .= ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.'; } if ($authors[$i]['mi']) { $ret .= ' ' . ucwords($authors[$i]['mi']) . '., '; } else { $ret .= ', '; } } else { //The author is a corporation and not a person $ret .= ', '; } } else { //There is only one author if ($authors[$i]['lname'] != 'Anonymous' || !$authors[$i]['lname'] && !$authors[$i]['fname'] && !$authors[$i]['mi']) { //The author is not Anonymous or blank $ret .= ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '. '; } if ($authors[$i]['mi']) { $ret .= ucwords($authors[$i]['mi']) . '. '; } } else { //The author is a corporation and not a person $ret .= '. '; } } } } elseif ($i >= 5) { //Sixth or more time through the loop if ($numAuthors > 7 && $i == 5) { //There are more than 7 authors and this is the sixth time through the loop $ret .= ' ' . ucwords($authors[$i]['lname']) . ', '; if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= Utility::firstInitial($authors[$i]['fname']) . '.'; } if ($authors[$i]['mi']) { $ret .= ' ' . ucwords($authors[$i]['mi']) . '.'; } $ret .= ', . . . '; } else { //The author is a corporation and not a person $ret .= ', . . . '; } } elseif ($numAuthors == 7 && $i == 5) { //There are 7 authors and this is the sixth time through the loop $ret .= ' ' . ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '. '; } if ($authors[$i]['mi']) { $ret .= ucwords($authors[$i]['mi']) . '., & '; } else { $ret .= ucwords($authors[$i]['mi']) . ', & '; } } else { //The author is a corporation and not a person $ret .= ', & '; } } elseif ($i + 1 == $count) { //This is the last time through the loop //If there are 6 authors add an ampersand before the name, otherwise do not if ($numAuthors == 6) { $ret .= ' & ' . ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '. '; } if ($authors[$i]['mi']) { $ret .= ucwords($authors[$i]['mi']) . '. '; } } else { //The author is a corporation and not a person $ret .= '. '; } } else { $ret .= ' ' . ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '. '; } if ($authors[$i]['mi']) { $ret .= ucwords($authors[$i]['mi']) . '. '; } } else { //The author is a corporation and not a person $ret .= '. '; } } } } else { if ($i + 1 == $count) { //This is the last time through the loop if ($numAuthors > 1) { //There is more than one author $ret .= ' & ' . ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.'; } if ($authors[$i]['mi']) { $ret .= ' ' . ucwords($authors[$i]['mi']) . '. '; } $ret .= ' '; } else { //The author is a corporation and not a person $ret .= '. '; } } else { //There is only one author if ($authors[$i]['lname'] != 'Anonymous' || !$authors[$i]['lname'] && !$authors[$i]['fname'] && !$authors[$i]['mi']) { //The author is not Anonymous or blank $ret .= ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '. '; } if ($authors[$i]['mi']) { $ret .= ucwords($authors[$i]['mi']) . '. '; } } else { //The author is a corporation and not a person $ret .= '. '; } } } } else { $ret .= ' ' . ucwords($authors[$i]['lname']); if ($authors[$i]['fname'] || $authors[$i]['mi']) { //The author is a person and not a corporation //Check for a hyphen in the first name $hyphentest = stripos($authors[$i]['fname'], '-'); if ($hyphentest != false) { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.-'; } else { $ret .= ', ' . Utility::firstInitial($authors[$i]['fname']) . '.'; } if ($authors[$i]['mi']) { $ret .= ' ' . ucwords($authors[$i]['mi']) . '.,'; } else { $ret .= ', '; } } else { //The author is a corporation and not a person $ret .= ', '; } } } } } return $ret; }