Skip to content

Instantly share code, notes, and snippets.

@rileymjohnson
Created August 29, 2018 13:02
Show Gist options
  • Save rileymjohnson/2227e8fcb5489482f898b6df59c2da5a to your computer and use it in GitHub Desktop.
Save rileymjohnson/2227e8fcb5489482f898b6df59c2da5a to your computer and use it in GitHub Desktop.
Bibtex work parser that lowercase's properties
/*
this is a snippet of the class that does this processing
*/
foreach ($bibliography as &$work){
foreach ($work as $key => $value) { //iterates over the properties of the work (an individual citation) one by one
if ($key != "author") { //does every property except author
$work[$key] = self::sentenceCase($work[$key], $key); //lowercases everything but the bracketed words
}
}
}
/*
this is the function that does the lowercasing and bracket exclusion.
it's probably not important, but this is it if you were curious.
*/
private function sentenceCase($sentence) {
preg_match_all('/(?<!\\\)\{(?!\\\)([^\}]+)}/', $sentence, $matches, PREG_OFFSET_CAPTURE);
$sentence = strtolower($sentence);
for ($i = 0; $i < count($matches[0]); $i++) {
$sentence = substr_replace($sentence, $matches[1][$i][0], $matches[0][$i][1] - (2 * $i), strlen($matches[0][$i][0]));
}
return ucfirst($sentence);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment