Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3966208
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:34:11+00:00 2026-05-20T03:34:11+00:00

I want to create a PDF chord chart when someone enters lyrics. The format

  • 0

I want to create a PDF chord chart when someone enters lyrics. The format the lyrics will be entered are like this:

[B]Some lyrics blah blah [D]more lyrics blah
Lyrics without any chord charts blah blah
Some lyrics [C]blah blah more [E]lyrics blah

I then want to extract each chord change so I can export and display it in a PDF like so:

B                     D
Some lyrics blah blah more lyrics blah
Lyrics without any chord charts blah blah
            C              E
Some lyrics blah blah more lyrics blah

I have everything else sorted. At the moment I just need to figure out how to extraxt all the text between each chord and the chord itself…

Here’s my current code:

$lyrics = '[B]Some lyrics blah blah [D]more lyrics blah
Lyrics without any chord charts blah blah
Some lyrics [C]blah blah more [E]lyrics blah';

$lyrics_html = '';
$x = 0;
$lyrics_lines = explode("\n", $lyrics);

// We've put each line into an array and now we will process each individually
foreach($lyrics_lines AS $lyrics_line) {

    $x++;
    if ($x > 1)
        $lyrics_html .= '<br />';

    // Check and see if an chords exist in this line
    preg_match_all('/\[(\w{1,3})\]/i',
    $lyrics_line,
    $out);

    if (!empty($out[0])) { // Found some chords, format it

        print_r($out);

    } else { // No chords found so just display the lyrics

        $lyrics_html .= $lyrics_line;

    }

}

echo $lyrics_html;

My following code selects the chords no worries

preg_match_all('/\[(\w{1,3})\]/i',
    $lyrics_line,
    $out);

But when I try to select text before and after the chords, I can’t seem to figure it out… I tried the following:

preg_match_all('/(\w+)\[(\w{1,3})\](\w+)/i',
    $lyrics_line,
    $out);

Any ideas what code I need so that I can select the chords and the lyrics so I can format them?

The sort of result that would be good is something like this:

Array
(
    [0] => Array
        (
        [0] => [B]
        [1] => [D]
        )

    [1] => Array
        (
        [0] => Some lyrics blah bla
        [1] => more lyrics blah
        )

)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:34:12+00:00Added an answer on May 20, 2026 at 3:34 am

    Interesting problem… Here’s a possible solution. I used the regular expression you had, but also used it to see how many spaces were required to pad the chords.

    <?php
    define('REGEX_CHORD', '[A-G].*?');
    
    $input = '[B]Some lyrics blah blah [D]more lyrics blah
    Lyrics without any chord charts blah blah
    Some lyrics [C]blah blah more [E]lyrics blah';
    
    $lines = explode("\n", $input);
    echo '<pre>';
    foreach($lines as $line) {
        $chordline = '';
        $tmpline = $line;
        while(preg_match('~(.*)\[('.REGEX_CHORD.')\]~', $tmpline, $matches)) {
            // Length before the chord shows how many spaces are required.
            $chordline = str_repeat(' ', strlen($matches[1]))
                    .$matches[2]
                    .substr($chordline, 
                        strlen($matches[2]) + 3 + strlen($matches[1]));
            $tmpline = $matches[1];
        }
        echo $chordline."\n";
        echo preg_replace('~\[('.REGEX_CHORD.')\]~', '', $line)."\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create a pdf using iText. The method which does this is a
i want to create a pdf page where i want to copy some text
I want to create a simple program that will convert a pdf to a
I want to create a PDF file using zend_pdf. But this way seems not
I want to create a PDF reader like iBooks. So that you can see
I want to create and insert a lot of images into a PDF and
I am using ASP.NET and I want to create a barcode in a pdf
I have a pdf file on my server. I want to create such link
i want create image animation , i have 50 images with png format now
I want to create a button which will run a previously ran function. ie

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.