Skip to content

Instantly share code, notes, and snippets.

@binki
Forked from xeoncross/PHP Sort by Scripture (Bible)
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binki/e2401d2affad7190195d to your computer and use it in GitHub Desktop.
Save binki/e2401d2affad7190195d to your computer and use it in GitHub Desktop.
<?php
function _scripturebooks() {
return array("Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy", "Joshua", "Judges", "Ruth", "1 Samuel", "2 Samuel", "1 Kings", "2 Kings", "1 Chronicles", "2 Chronicles", "Ezra", "Nehemiah", "Esther", "Job", "Psalm", "Proverbs", "Ecclesiastes", "Song of Solomon", "Isaiah", "Jeremiah", "Lamentations", "Ezekiel", "Daniel", "Hosea", "Joel", "Amos", "Obadiah", "Jonah", "Micah", "Nahum", "Habakkuk", "Zephaniah", "Haggai", "Zechariah", "Malachi", "Matthew", "Mark", "Luke", "John", "Acts", "Romans", "1 Corinthians", "2 Corinthians", "Galatians", "Ephesians", "Philippians", "Colossians", "1 Thessalonians", "2 Thessalonians", "1 Timothy", "2 Timothy", "Titus", "Philemon", "Hebrews", "James", "1 Peter", "2 Peter", "1 John", "2 John", "3 John", "Jude", "Revelation");
}
function scripturesort($a, $b) {
if ($a == $b) {return 0;}
$order=_scripturebooks();
$fa = $a;
$fb = $b;
if (preg_match('/^..(.+?)\b/', $a, $matches))
$a = trim(substr($matches[0],0,4));
else
$a = '';
if (preg_match('/^..(.+?)\b/', $b, $matches))
$b = trim(substr($matches[0],0,4));
else
$b = '';
similar_text($a, $b, $p);
if($p > 75) {
preg_match('/(.+?)([\d]+)/', $fa, $matches);
$na = intval(@$matches[2]);
preg_match('/(.+?)([\d]+)/', $fb, $matches);
$nb = intval(@$matches[2]);
if($na == $nb) {
preg_match('/(.+?)([\d]+).*?:([\d]+)/', $fa, $matches);
$n2a = intval(@$matches[3]);
preg_match('/(.+?)([\d]+).*?:([\d]+)/', $fb, $matches);
$n2b = intval(@$matches[3]);
return ($n2a < $n2b) ? -1 : 1;
} else
return ($na < $nb) ? -1 : 1;
}
$position = empty($a) ? NULL : key(preg_grep('/^'.$a.'(.*)\b/', $order));
$position2 = empty($b) ? NULL : key(preg_grep('/^'.$b.'(.*)\b/', $order));
if ($position2!==NULL && $position!==NULL) {return ($position < $position2) ? -1 : 1;}
if($position!==NULL) {return -1;}
if($position2!==NULL) {return 1;}
return ($a < $b) ? -1 : 1;
}
$scriptures = array(
'Genesis',
'Genesis 1:12',
'Genesis 1:1',
'Revelation',
'Leviticus 2:10',
'Exodus 3:4',
'Nonexistent',
'Job',
'',
'Matthew',
);
usort($scriptures, 'scripturesort');
var_dump($scriptures);
/**
* Reduce a scripture reference into something that an SQL ORDER BY
* clause should correctly order as either a numeric or non-numeric.
*
* Derived from https://gist.github.com/Xeoncross/9534124.
*
* Returns a string with no whitespace.
*/
function scripture2db_orderable($a) {
$order=_scripturebooks();
$fa = $a;
if (preg_match('/^..(.+?)\b/', $a, $matches))
$a = trim(substr($matches[0],0,4));
else
$a = '';
preg_match('/(.+?)([\d]+)/', $fa, $matches);
$na = intval(@$matches[2]);
preg_match('/(.+?)([\d]+).*?:([\d]+)/', $fa, $matches);
$n2a = intval(@$matches[3]);
$position = empty($a) ? NULL : key(preg_grep('/^'.$a.'(.*)\b/', $order));
if ($position === NULL)
$position = 999-1;
return sprintf('%03d.%03d:%03d', $position+1, $na, $n2a);
}
$scriptures_orderable = array();
foreach ($scriptures as $scripture)
{
$key = scripture2db_orderable($scripture);
$scriptures_orderable += array($key => array());
$scriptures_orderable[$key][] = $scripture;
}
ksort($scriptures_orderable);
var_dump($scriptures_orderable);
@xeoncross
Copy link

If you edit this gist and rename the file ".php" then gist will highlight the code for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment