argument('file'); $handle = fopen($filename, "r"); $theSong = [ 'text' => '', 'verse' => '', 'number' => FALSE, ]; if ( !$handle ){ $this->error("Couldn't read file '$filename'"); return 1; } while (($line = fgets($handle)) !== false) { // If we see a number, then that is what song we are on. $matches = array(); if ( preg_match("(^(X?C?B?\d+)\. )", $line, $matches) ) { if ( $theSong['text'] && $theSong['number'] && $theSong['title'] && ( $theSong['number'] != $matches[1] ) ) { Song::create( $theSong ); $this->line( "Created $theSong[title]" ); $theSong = [ 'text' => '', 'verse' => '', 'key' => NULL, 'author' => NULL, ]; } $theSong['number'] = $matches[1]; $theSong['title'] = trim($line); $this->line( "Creating $theSong[title]..." ); } $theSong['text'] .= $line; if ( preg_match( "/\{p?\d*\((.+m?)\)\}/", $line, $matches) || preg_match("/^{Key: ?([^ ]*).*}/i", $line, $matches) ) { $theSong['key'] = $matches[1]; } if ( preg_match( AUTHOR_REGEX, $line, $matches ) ) { $theSong['author'] = $matches[1]; } } fclose($handle); Song::create( $theSong ); } }