Php 使用preg_replace替换和弦,但不替换合唱和桥牌
我有一些文本如下所示:Php 使用preg_replace替换和弦,但不替换合唱和桥牌,php,preg-replace,Php,Preg Replace,我有一些文本如下所示: [Verse 1] [B]This is a song with chords [D]and lyrics Blah [Am7+9d]blah blah [C]blah blah [Verse 2] This [C]is the verse This is the [G]verse [Chorus] This is [E#]the Chorus This is the Chorus [Bridge] This is [F]the bridge This is [Am
[Verse 1]
[B]This is a song with chords [D]and lyrics
Blah [Am7+9d]blah blah [C]blah blah
[Verse 2]
This [C]is the verse
This is the [G]verse
[Chorus]
This is [E#]the Chorus
This is the Chorus
[Bridge]
This is [F]the bridge
This is [Am]the bridge
我想替换所有和弦([A]、[Am]、[A]、[B]、[C]等等,一直到[G]),这样我就可以显示歌词了
以下是我目前的代码,几乎可以正常工作:
$line = preg_replace('~\[([A-G].*?)\]~', '', $line);
这很有效,但它删除了[Chorus]和[Bridge],因为它们以C和B开头
我如何使它仍然完美地工作,但不删除[Chorus]和[Bridge]?$line=preg\u replace(“~\[((?!Chorus | Bridge)[A-G].\]~,”,“$line)代码>$line=preg\u replace(“~\[(?!Chorus | Bridge)[A-G].*?\]~,”$line)代码>一种不同的(更通用的)解决方案,基于对从提供的文本片段中派生的数据的假设
if (preg_match("/^\[[^\[\]]*\]$/", $line) === 0)
$line = preg_replace("/\[.*\]/", "", $line);
一个不同的(更通用的)解决方案,基于对从提供的文本片段派生的数据的假设
if (preg_match("/^\[[^\[\]]*\]$/", $line) === 0)
$line = preg_replace("/\[.*\]/", "", $line);
在出现[chorus]等情况时,忽略大小写可能是明智的:$line=preg_replace(“~[(?!chorus|Bridge)[A-G].*i',”$line);Am7或F#或Asus7或Am7+9d如何?如果出现[合唱]等情况,则忽略大小写可能是明智的:$line=preg_替换(“~[(?!合唱|桥)[A-G].]~i',”,“$line”);那么Am7或F#或Asus7或Am7+9d呢?