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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:01:12+00:00 2026-06-15T01:01:12+00:00

I’m new to PHP and am trying to parse certain text from a txt

  • 0

I’m new to PHP and am trying to parse certain text from a txt file, and then insert the text into a MySQL database. So, let’s get more specific. The file’s format is as such, and it is repeated through the document end. the ellipses represent the previous and next tones.

...

[Tone27]                            
Atone = 707.3                        
Btone = 746.8                        
Btonelength = 3                        
Btonedebounce = 1                    
Description = Fire Department 1                
mp3_Emails = email@address.com,email2@address.com,email3@address.com          
amr_Emails = email2@textmessaging.com,email1@textmessaging.com        
alert_command = c:\test.bat                
post_email_command = c:\test2.bat            
radio_frequency = 154.475    
exclude_from = 13:25                        
exclude_to = 13:35                        
exclude_emails = email2@textmessaging.com,email2@address.com

... 

What I want to do is parse the first items(e.g. ‘[tone27]’) in each “tone block” from the file and insert it into the first field of a NEW row in the db. I then need to evaluate what comes before each line’s ” = “, for instance “Atone,” and insert what comes after that line’s ” = “, for instance “707.3” into a field by that name. so, this row may look like this in the db:

$id | [tone27] | 707.3 |746.8 | 3 | 1 | Fire Department 1 |email1@x.com,email2@x.com,e...|...

and so on…

i’ve been able to isolate each thing by performing string functions, but am unsure of how to set up a loop that would insert each value properly. Here’s the code I used to isolate them, but it’s not helping at all with actually getting them into the database.

$txt_file    = file_get_contents('config/tones.txt');
$rows        = explode("\n", $txt_file);
foreach($rows as $row => $data)
{   
$row_data = explode(' = ', $data);

if ((isset($row_data[0])) && ($row_data[0] !== " " )){
    $info[$row]['attribute'] = $row_data[0];
    $info_attribute = trim($info[$row]['attribute']);
}
if (isset($row_data[1])){
    $info[$row]['value']         = $row_data[1];
    $info_value = trim($info[$row]['value']);
    //display data
    echo 'Row ' . $row . ' Attribute: ' . $info_attribute . '<br />';
    echo 'Row ' . $row . ' Value: ' . $info_value . '<br />';
} elseif (($info[$row]['attribute']) && (!empty($info_attribute))) {
    echo "<br>";
    echo 'Row ' . $row . ' Attribute: ' . $info_attribute . '<br />';
    continue;
}      

I’M A NOOB, NO DOUBT. I’M LOST. Thanks in advance for your help!!!

****|| EDIT ||****

Thanks for all of the excellent answers! here’s what I’ve resultingly come up with. No queries yet, just a simple dash of the read portion of CRUD, but the code will be the same, only with queries. A big thanks to @leepowers for introducing me to the wonderful parse_ini_file() function.

foreach(parse_ini_file("config/tones.txt", true) as $k => $v){
extract($v, EXTR_SKIP);
echo "<br>";
echo $k . "<br>"; 
foreach($v as $sv => $ssv){ 

    $lcase_sv = strtolower($sv); 
    if (trim($lcase_sv) == 'amr_emails'){ 
        echo "sv: amr_Emails:<br>"; 
        echo "ssv:<br>";
        $eA = explode(',', trim($ssv));
        foreach($eA as $eK => $eV){ 
            echo "email" . filter_var($eK + 1, FILTER_SANITIZE_NUMBER_INT) . ": " . $eV . "<br>"; 
        }
    } elseif (trim($lcase_sv) == 'mp3_emails'){ 
        echo "ssv:<br>";
        $eA = explode(',', trim($ssv));
        foreach($eA as $eK => $eV){
            echo "email" . filter_var($eK + 1, FILTER_SANITIZE_NUMBER_INT) . ": " . $eV . "<br>";
        }
    }else {
        echo "sv: " . $sv .", " . "s: " . $ssv . "<br>"; 
    }
}

}

  • 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-06-15T01:01:14+00:00Added an answer on June 15, 2026 at 1:01 am

    Use parse_ini_file to load the data structure into an array.

    From there you can build and execute SQL statements:

    $entries = parse_ini_file("config/tones.txt", true);
    foreach ($entries as $section => $fields) {
      extract($fields, EXTR_SKIP);
      $sql = "INSERT INTO mytable (section, atone, btone) VALUES ($section, '$Atone', '$Btone'";
       ....
    }
    

    Of course, you’ll need to prepare and escape the SQL statement before executing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a reasonable size flat file database of text documents mostly saved in
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

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.