Посмотрите, существует ли значение в группе в регулярном выражении, а затем манипулирует ею в тексте

Я пытаюсь переписать функцию в javascript на php, но она не работает. Может быть, кто-то здесь может сказать мне, где я ошибаюсь? Заранее спасибо.

Javascript … (рабочая функция)

var match; var chords = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C']; var chords2 = ['C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C']; var chordRegex = /(?:C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B)/g; var rec = /\{(.*?)\}/g; // gets all info between { and } function transposeUp(){ var html = $('.yui-editor-editable').contents().find('body'); //gets the contents var matches = $(html).html().match(rec);/////Gets all matches within the html var text = $(html).html(); // html of the html var for (var i = 0; i < matches.length; i++) { /////foreach match do ///// initializes variables ///// var currentChord = String(matches[i]); //// sets current chord currentChord = currentChord.substring(currentChord.indexOf("{") + 1, currentChord.indexOf("}")); //sets current chord cont. var output = ""; var parts = currentChord.split(chordRegex); // splits currentChord into parts in the chordRegex var index = 0; ///////////////////////////////// while (match = chordRegex.exec(currentChord)) { // while the match is equal to the currentChord do var chordIndex = chords2.indexOf(match[0]); // find the position of the matched chord within the chords2 array output += parts[index++] + chords[chordIndex + 1]; // build the output } output += parts[index]; text = text.replace(matches[i], '{'+output+'}'); //replace the text with the new chords } $(html).html(text); // set the html value as the new text } 

PHP …

 function transposeUp($songchart){ $chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C'); $chords2 = array('C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C'); $chordRegex = "/(?:C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B)/"; $matches = preg_match_all('#{.*(.*)}#U', $songchart, $chords); $chord = $chords[1]; foreach($chord as $key => $value){ $output = ''; $parts = preg_split($chordRegex, $value); $index = 0; while(preg_match($chordRegex, $value, $note)){ $chordIndex = strpos($chords2, $note[0]); $output .= $parts[$index++] . $chords1[$chordIndex + 1]; } $output .= $parts[$index]; $songchart = preg_replace($value, $output, $songchart); } return($songchart); } , function transposeUp($songchart){ $chords1 = array('C','C#','D','D#','E','F','F#','G','G#','A','A#','B','C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C'); $chords2 = array('C','Db','D','Eb','E','F','Gb','G','Ab','A','Bb','B','C','C#','D','D#','E','F','F#','G','G#','A','A#','C'); $chordRegex = "/(?:C#|D#|F#|G#|A#|Db|Eb|Gb|Ab|Bb|C|D|E|F|G|A|B)/"; $matches = preg_match_all('#{.*(.*)}#U', $songchart, $chords); $chord = $chords[1]; foreach($chord as $key => $value){ $output = ''; $parts = preg_split($chordRegex, $value); $index = 0; while(preg_match($chordRegex, $value, $note)){ $chordIndex = strpos($chords2, $note[0]); $output .= $parts[$index++] . $chords1[$chordIndex + 1]; } $output .= $parts[$index]; $songchart = preg_replace($value, $output, $songchart); } return($songchart); } 

Solutions Collecting From Web of "Посмотрите, существует ли значение в группе в регулярном выражении, а затем манипулирует ею в тексте"

 $matches = preg_match_all('#{.*(.*)}#U', $songchart, $chords); 

должно быть:

 $matches = preg_match_all('#\{(.*)\}#U', $songchart, $chords); 

В вашем регулярном выражении первое .* Потребляло все внутри фигурных скобок, поэтому второе ничего не получало.

Я не понимаю цикл while. $value никогда не изменяется, так как цикл будет завершаться?

 $chordIndex = strpos($chords2, $note[0]); 

должно быть:

 $chordIndex = array_search($chords2, $note[0]); 

поскольку strpos предназначен для поиска в строках, а не в массивах.

 $songchart = preg_replace($value, $output, $songchart); 

должно быть:

 $songchart = str_replace($value, $output, $songchart); 

поскольку $value не является регулярным выражением.

Я не уверен во всем, что это делает, но я подозреваю, что это может быть упрощено с помощью preg_replace_callback или, возможно, даже одного str_replace с массивами для аргументов поиска и замены.