/** * The template for displaying all pages. * * Template Name: Notes Converter */ //if (!is_admin()) exit(); ?> if (isset($_POST['notes'])) : $notes = $_POST['notes']; $shift = $_POST['shift']; processNotes($notes, $shift); //echo wpautop($_POST['notes']); ?> endif; ?>
function processNotes($old_notes, $shift) { $old_notes = str_replace('A#-', 'A#', $old_notes); $old_notes = str_replace('B-', 'B', $old_notes); $old_notes = str_replace('A#', 'A#+', $old_notes); $old_notes = str_replace('B#', 'X#', $old_notes); $old_notes = str_replace('B', 'B+', $old_notes); //It will mess up B# too $old_notes = str_replace('X#', 'B#', $old_notes); $array_o = ['A','A#','B','C','C#','D','D#','E','F','F#','G','G#']; $new_notes = str_split($old_notes); $final = ""; foreach ($new_notes as $note) { if (in_array($note, $array_o)) { //shift the notes here $current_index = array_search($note, $array_o); if (array_key_exists($current_index+1, $array_o) && !array_key_exists($current_index+2, $array_o)) { $current_index = -2; } if (!array_key_exists($current_index+1, $array_o) && !array_key_exists($current_index+2, $array_o)) { $current_index = -1; } $new_note = $array_o[$current_index+$shift]; $final .= $new_note; } elseif ($note == "\r") { $final .= "