Read contents of a text file and parse into database?

Hello All,

Looking for some help. I’m trying to put together a script which:

  • Looks in a folder for text files
  • Inserts the data in the text files into a database table row by row.

So here’s my text from one .txt file:

and here’s my code thus far.


/* Define and Open the Directory */
$path	= '/home/******/parsers/*******/write/';
$dir	= opendir($path);

/* Loop through the directory using a while loop if $dir exists */
while (false !== ($file = readdir($dir))) {

//Read the files in the directory
$the_text = file_get_contents($path.$file);

  //Now need to insert into the database. This is the problem area!!
  list($headline, $subheading, $the_text) = explode(PHP_EOL, $the_text); 
  
  $SQL = "INSERT INTO testing_text headline,subheading,the_text) VALUES ('$headline','$subheading','$the_text')";
  $QUE = mysql_query($SQL);
  
echo '<pre>'; print_r($the_text); echo '</pre>';

Now here’s the issue, I currently use PHP_EOL to insert each line in the database, but as you can see I have line breaks from the above image, so that will not work – especially with $the_text which I want to insert into one column.

So my question is can anyone kindly help me change my code so it inserts in the database like so:

Change
explode(PHP_EOL, $the_text)
to
preg_explode("
+", $the_text, 3);

Also, nobody uses PHP_EOL.

Thanks, can you explain here what that’s doing as i’d like to understand rather than just do it?