gpt4 book ai didi

php - 查看正则表达式中的组中是否存在某个值,然后在文本中对其进行操作

转载 作者:行者123 更新时间:2023-11-28 02:41:07 24 4
gpt4 key购买 nike

我正在尝试将 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);
}

最佳答案

$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和替换参数。

关于php - 查看正则表达式中的组中是否存在某个值,然后在文本中对其进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12632901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com