Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I use regex to parse this chord scheme?

Tags:

regex

php

Consider the following input as an example:

[Ami]Song lyrics herp derp [F]song lyrics continue
[C7/B]Song lyrics continue on another [F#mi7/D]line

I need to parse the above and echo it as the following:

<div class="chord">Ami</div>Song lyrics herp derp <div class="chord">F</div>song lyrics continue
<div class="chord">C7/B</div>Song lyrics continue on another <div class="chord">F#mi7/D</div>line

So basically, I need to:

1) change [ to <div class="chord">,

2) then append the content of the brackets,

3) then change ] to </div>.

... using PHP 5.3+.

like image 708
Frantisek Avatar asked Oct 29 '11 02:10

Frantisek


1 Answers

This will work.

$tab = "[Ami]Song lyrics herp derp [F]song lyrics continue
[C7/B]Song lyrics continue on another [F#mi7/D]line";

echo str_replace(
    array('[', ']'),
    array('<div class="chord">','</div>'),
    $tab
);
like image 183
Book Of Zeus Avatar answered Oct 14 '22 03:10

Book Of Zeus