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>";
}
}
}
Use
parse_ini_fileto load the data structure into an array.From there you can build and execute SQL statements:
Of course, you’ll need to prepare and escape the SQL statement before executing it.