first(); $lines = explode( "\n", $song['text'] ); $newText = ''; foreach( $lines as $line ){ $line = htmlspecialchars( $line ); if ( $this->chordline( $line ) ) { $transp = 0; $class = "tabs chord$transp"; $line = str_replace( array('{','}'), array('{', "}" ), $line ); $newText .= "" . $line . "\n"; } else { $newText .= "$line\n"; } } $song['escapedText'] = $newText; return view('song', ['song' => $song, 'transp' => 0 ] ); } /** * @brief Determine whether or not this line contains chords. */ private function chordline($line) { $chords = array( "C","C#","D","D#","E","F","F#","G","G#","A#","B",//"A", "Db", "Eb", "Gb", "Bb",//"Ab", "Cm", "Dm", "Fm", "Gm", "Bm", //"Em", "Am", ); $ambiguous = array( "Ab", "Em", "Am", "A" ); $line = str_replace(array('/','7', "\n", '2', '4'), ' ', $line); $tokens = array_filter(explode(' ', $line)); $badtokens = 0; $ambtokens = 0; $goodtokens = 0; foreach ($tokens as $token) { if( in_array( substr($token, 0,2), $chords ) ) $goodtokens++; else if ( in_array( substr( $token, 0,2), $ambiguous) ) $ambtokens++; else if( $badtokens > 10 ) return FALSE; else $badtokens++; } return ($goodtokens *2)+ $ambtokens >= $badtokens; } }