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 7548865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:48:21+00:00 2026-05-30T09:48:21+00:00

Ok so I have a text file that will change regularly that I need

  • 0

Ok so I have a text file that will change regularly that I need to scrape to display on screen and potentially insert into a database. The text is formatted as follows:

"Stranglehold"
Written by Ted Nugent
Performed by Ted Nugent
Courtesy of Epic Records
By Arrangement with
Sony Music Licensing
"Chateau Lafltte '59 Boogie"
Written by David Peverett
and Rod Price
Performed by Foghat
Courtesy of Rhino Entertainment
Company and Bearsville Records
By Arrangement with
Warner Special Products

I only need the song title (the information between the quotes), who it is written by and who it is performed by. As you can see the written by lines can be more than one row.

I’ve searched through the questions and this one is similar Scraping a plain text file with no HTML? and I was able to modify the solution https://stackoverflow.com/a/8432563/827449 below so that it will at least find the information between the quotes and put those in the array. However I can’t figure out where and how to put the next preg_match statements for the written by and performed by so that it will add it to the array with the correct information, assuming I have the right regex of course. Here is the modified code.

<?php
$in_name = 'in.txt';
$in = fopen($in_name, 'r') or die();

function dump_record($r) {
    print_r($r);
}
    $current = array();
    while ($line = fgets($fh)) {

        /* Skip empty lines (any number of whitespaces is 'empty' */
        if (preg_match('/^\s*$/', $line)) continue;

        /* Search for 'things between quotes' stanzas */
        if (preg_match('/(?<=\")(.*?)(?=\")/', $line, $start)) {
            /* If we already parsed a record, this is the time to dump it */
            if (!empty($current)) dump_record($current);

        /* Let's start the new record */
        $current = array( 'id' => $start[1] );
    }
    else if (preg_match('/^(.*):\s+(.*)\s*/', $line, $keyval)) {
        /* Otherwise parse a plain 'key: value' stanza */
        $current[ $keyval[1] ] = $keyval[2];
    }
    else {
        error_log("parsing error: '$line'");
    }
}
/* Don't forget to dump the last parsed record, situation
 * we only detect at EOF (end of file) */
if (!empty($current)) dump_record($current);

fclose($in);

Any help would be great as I am now over my head with my limited PHP and Regex knowledge.

  • 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-30T09:48:22+00:00Added an answer on May 30, 2026 at 9:48 am

    How about:

    $str =<<<EOD
    "Stranglehold"
    Written by Ted Nugent
    Performed by Ted Nugent
    Courtesy of Epic Records
    By Arrangement with
    Sony Music Licensing
    "Chateau Lafltte '59 Boogie"
    Written by David Peverett
    and Rod Price
    Performed by Foghat
    Courtesy of Rhino Entertainment
    Company and Bearsville Records
    By Arrangement with
    Warner Special Products
    
    EOD;
    
    preg_match_all('/"([^"]+)".*?Written by (.*?)Performed by (.*?)Courtesy/s', $str, $m, PREG_SET_ORDER);
    print_r($m);
    

    output:

    Array
    (
        [0] => Array
            (
                [0] => "Stranglehold"
    Written by Ted Nugent
    Performed by Ted Nugent
    Courtesy
                [1] => Stranglehold
                [2] => Ted Nugent
    
                [3] => Ted Nugent
    
            )
    
        [1] => Array
            (
                [0] => "Chateau Lafltte '59 Boogie"
    Written by David Peverett
    and Rod Price
    Performed by Foghat
    Courtesy
                [1] => Chateau Lafltte '59 Boogie
                [2] => David Peverett
    and Rod Price
    
                [3] => Foghat
    
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script that will convert a text file into a resource file,
I have a php function I wrote that will take a text file and
I have a plain text file that I need to read in using C#,
I have Text file that contains data separated with a comma , . How
I have a text file that contains a long list of entries (one on
I have a text file that contains localized language strings that is currently encoded
I have a text file that is encoded in UTF-8. I'm reading it in
I have a text file that I want to edit using Java. It has
I have a text file that contains a list of filenames, minus the extension,
I have a text file that looks a bit like: random text random text,

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.