Mr Chris Posted March 30, 2010 Share Posted March 30, 2010 Hello All, Wondering if anyone can Kindly help. I have several html files like so provided to me by a client: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>This is the title</title> </head> <body> <table width="400" border="0"> <tr> <td><h1>This is the title</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque tincidunt tristique felis, a lobortis quam iaculis vitae. Donec lobortis mattis purus ac accumsan. Cras eget risus sed nunc bibendum rutrum. Praesent luctus, tellus at congue tincidunt, leo lacus aliquet purus, in pretium massa lectus ut neque. Phasellus id tortor turpis. Sed viverra dapibus elit placerat dictum.</p> </td> </tr> </table> </body> </html> Now what I want to do is drop this file in a folder then read each file and insert the data from: - inbetween the <h1></h1> tags - inbetween the <p></p> tags Now i've got the script working so it opens the files in a folder and reads them, but I don't know how to ask php to grab the contents inbetween these two tags (<h1></h1 & <p></p>) and place them in a MYSQL database table. Can anyone help? Thanks <?php /* Define and Open the Directory */ $path = '/home/************************/'; $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: one.txt, two.txt, three.txt */ $the_text = file_get_contents($path.$file); /* This is where I am Stuck /* Now need to Grab the contents inbetween <h1></h1> and assign to $headline /* And between the <p></p> tag and assign to $headline and $the_text /* But dont know how to do it /* Insert into DB*/ $SQL = "INSERT INTO testing_text (headline,the_text) VALUES ('$headline','$the_text')"; $QUERY = mysql_query($SQL); } ?> Link to comment https://forums.phpfreaks.com/topic/196967-read-files-in-directory-post-to-mysql/ Share on other sites More sharing options...
the182guy Posted March 30, 2010 Share Posted March 30, 2010 Here's one way, it uses explode() to parse the content of the H1 and the P tags. This will work if there is one H1 tag and one P tag. while (false !== ($file = readdir($dir))) { /* Read the files in the directory: one.txt, two.txt, three.txt */ $contents = file_get_contents($path.$file); /* This is where I am Stuck /* Now need to Grab the contents inbetween <h1></h1> and assign to $headline /* And between the <p></p> tag and assign to $headline and $the_text /* But dont know how to do it */ $temp = explode("<h1>", $contents); $temp = explode("</h1>", $temp[1]); $headline = $temp[0]; $temp = explode("<p>", $contents); $temp = explode("</p>", $temp[1]); $the_text = $temp[0]; /* Insert into DB*/ $SQL = "INSERT INTO testing_text (headline,the_text) VALUES ('$headline','$the_text')"; $QUERY = mysql_query($SQL); } Link to comment https://forums.phpfreaks.com/topic/196967-read-files-in-directory-post-to-mysql/#findComment-1034042 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.