Skip to content

Instantly share code, notes, and snippets.

@jmather
Created March 30, 2011 14:34
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 jmather/894506 to your computer and use it in GitHub Desktop.
Save jmather/894506 to your computer and use it in GitHub Desktop.
Convert KJB to array
<?php
function processFile($file)
{
$file = file_get_contents($file);
$book = array();
list($junk, $cont) = explode('<hr>', $file, 2);
$cont = trim($cont);
preg_match('/<h2>(.*)<\/h2>/U', $cont, $matches);
$book['title'] = $matches[1];
$book['chapters'] = array();
preg_match_all('/<h3>(.*)<\/h3>/U', $cont, $matches);
foreach($matches[0] as $idx => $chapter_name)
{
$chapter = array();
$chapter['name'] = 'Chapter '.($idx + 1);
$chapter['verses'] = array();
// get chapter text
$begin_pos = strpos($cont, $chapter_name);
if (isset($matches[0][($idx + 1)]))
{
$end_pos = strpos($cont, $matches[0][($idx + 1)]);
$chapter_text = substr($cont, $begin_pos, $end_pos);
} else {
$end_pos = null;
$chapter_text = substr($cont, $begin_pos);
}
list($text, $junk) = explode('<hr>', $chapter_text);
$lines = explode('<br>', $text);
foreach($lines as $line)
{
$l = trim($line);
if (strpos($l, ']') === false)
{
continue;
}
list($junk, $verse) = explode(']', trim($line));
$chapter['verses'][] = $verse;
}
$book['chapters'][] = $chapter;
}
return $book;
}
$info = processFile('text');
print_r($info);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment