chords2halftone.php
changeset 0 16db14829751
child 4 bb3789db954a
equal deleted inserted replaced
-1:000000000000 0:16db14829751
       
     1 <?php
       
     2 
       
     3 if ( isset($_POST['text']) )
       
     4 {
       
     5 	$text = explode("\n", str_replace("\r\n", "\n", trim($_POST['text'])));
       
     6 	$chordline = false;
       
     7 	echo "<pre>";
       
     8 	foreach ( $text as $i => $line )
       
     9 	{
       
    10 		if ( $i == 0 )
       
    11 		{
       
    12 			echo "&lt;halftone title=\"" . htmlspecialchars(htmlspecialchars($line)) . "\"&gt;\n";
       
    13 		}
       
    14 		else if ( trim($line) == "" )
       
    15 		{
       
    16 			// do nothing
       
    17 		}
       
    18 		else if ( preg_match('/^(.*?(?:Verse|Chorus|Bridge).*?):\s*$/i', $line, $match) )
       
    19 		{
       
    20 			if ( $chordline )
       
    21 			{
       
    22 				$chordstack = preg_split("/([ \t]+)/", $chordline);
       
    23 				echo '(' . implode(") (", $chordstack) . ")\n";
       
    24 				$chordline = false;
       
    25 			}
       
    26 			
       
    27 			if ( $i > 3 )
       
    28 				echo "\n";
       
    29 			echo "= {$match[1]} =\n";
       
    30 		}
       
    31 		else if ( preg_match('/^\((.*?(?:Verse|Chorus|Bridge).*?)\)$/i', $line, $match) )
       
    32 		{
       
    33 			if ( $chordline )
       
    34 			{
       
    35 				$chordstack = preg_split("/([ \t]+)/", $chordline);
       
    36 				echo '(' . implode(") (", $chordstack) . ")\n";
       
    37 				$chordline = false;
       
    38 			}
       
    39 			echo "\n[{$match[1]}]\n";
       
    40 		}
       
    41 		else if ( preg_match('/^(\s*([A-G][#b]?(?:m?7?|2|add9|sus4|[Mm]aj[79])?)(\/[A-G][#b]?)?\s*)*$/', $line) )
       
    42 		{
       
    43 			if ( $chordline )
       
    44 			{
       
    45 				// we have two chord lines in a row... treat the last one as a transition
       
    46 				$chordstack = preg_split("/([ \t]+)/", $chordline);
       
    47 				echo '(' . implode(") (", $chordstack) . ")\n";
       
    48 			}
       
    49 			
       
    50 			// chord line
       
    51 			$chordline = $line;
       
    52 		}
       
    53 		else if ( $chordline && trim($line) )
       
    54 		{
       
    55 			// combine chord line with text line
       
    56 			$chordline = preg_split('/([ \t]+)/', $chordline, -1, PREG_SPLIT_DELIM_CAPTURE);
       
    57 			
       
    58 			if ( count($chordline) >= 2 && preg_match('/^\s*$/', $chordline[0]) && preg_match('/^\s*$/', $chordline[1]) )
       
    59 			{
       
    60 				$merger = array_shift($chordline);
       
    61 				$chordline[0] .= $merger;
       
    62 			}
       
    63 			
       
    64 			$chordstack = array();
       
    65 			for ( $j = 0; $j < count($chordline); $j++ )
       
    66 			{
       
    67 				if ( $j == 0 && !preg_match('/^\s*$/', $chordline[$j]) )
       
    68 				{
       
    69 					$chordstack[] = "({$chordline[$j]})";
       
    70 					if ( isset($chordline[$j+1]) )
       
    71 					{
       
    72 						$chordline[$j+1] .= str_repeat(' ', strlen($chordline[$j]));
       
    73 					}
       
    74 					continue;
       
    75 				}
       
    76 				// insert line up until this chord
       
    77 				$chordstack[] = substr($line, 0, strlen($chordline[$j]));
       
    78 				// chomp off the front of the line
       
    79 				$line = substr($line, strlen($chordline[$j]));
       
    80 				// insert this chord
       
    81 				if ( isset($chordline[++$j]) )
       
    82 				{
       
    83 					if ( !empty($chordline[$j]) )
       
    84 					{
       
    85 						$chordstack[] = "({$chordline[$j]})";
       
    86 						if ( isset($chordline[$j+1]) )
       
    87 						{
       
    88 							$chordline[$j+1] .= str_repeat(' ', strlen($chordline[$j]));
       
    89 						}
       
    90 					}
       
    91 				}
       
    92 			}
       
    93 			$chordstack[] = $line;
       
    94 			echo implode("", $chordstack) . "\n";
       
    95 			$chordline = false;
       
    96 		}
       
    97 		else
       
    98 		{
       
    99 			// assume it's a lyric line without chords...?
       
   100 			echo "$line\n";
       
   101 		}
       
   102 	}
       
   103 	
       
   104 	echo "&lt;/halftone&gt;";
       
   105 	echo "</pre>";
       
   106 	exit;
       
   107 }
       
   108 
       
   109 ?>
       
   110 <form method="post">
       
   111 <p><textarea name="text" rows="20" cols="100"></textarea></p>
       
   112 <p><input type="submit" value="Make Halftone" /></p>
       
   113 </form>