Skip to content

Instantly share code, notes, and snippets.

@pantor
Created October 23, 2014 14:46
Show Gist options
  • Save pantor/426e54df67fee56dfddf to your computer and use it in GitHub Desktop.
Save pantor/426e54df67fee56dfddf to your computer and use it in GitHub Desktop.
A simple PHP script that converts the ChordPro syntax to plain text.
<?php
$test = "Hello [C]song, this is [Am]wonderful. [E] [Am]";
print($test."\n");
$helper_line = $test;
$chord_line = "";
$lyrics_line = "";
$current_chord_length = 0;
$currently_chord = false;
$currently_lyrics = true;
while (strlen($helper_line) > 0) {
$token = substr($helper_line, 0, 1);
if ($token == "]" && $currently_chord) {
$currently_chord = false;
}
if ($token == "[") {
$currently_lyrics = false;
while ($current_chord_length > 0) {
$lyrics_line = $lyrics_line." ";
if ($current_chord_length == 1) {
$lyrics_line = $lyrics_line." ";
$chord_line = $chord_line." ";
}
$current_chord_length -= 1;
}
if (substr($chord_line, -1) !== " ") {
$chord_line = $chord_line." ";
$lyrics_line = $lyrics_line." ";
}
}
if ($currently_chord) {
$chord_line = $chord_line.$token;
$current_chord_length += 1;
}
if ($currently_lyrics) {
$lyrics_line = $lyrics_line.$token;
if ($current_chord_length == 0) {
$chord_line = $chord_line." ";
} else {
$current_chord_length -= 1;
}
}
if ($token == "[") {
$currently_chord = true;
}
if ($token == "]" && !$currently_lyrics) {
$currently_lyrics = true;
}
$helper_line = substr($helper_line, 1);
}
print("\n".$chord_line."\n");
print($lyrics_line."\n");
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment