Ok so I have been futzing with this for a while tonight and I cant just get this right... So here it goes. I have some text files on my server. I would like a script to open the file and extract the data within in and set it up for an insert into a database. Let me give you an example. These are just standard text files so... Couple things about that are... 1.) Notice the spacing in between the end of one offer details and the title of the next one. 2.) Would like to get the insert to be INSERT INTO offers (offer_name, credit, cost, cr_time, site_id) VALUES('Blockbuster','1','9.99','Within 1 Day - 3 Days','32'); So the script would quickly loop thru the file(s) and remove all the data except for the above example and then output the loop to an insert. I can get the file open part and lil trouble with the loop and I can handle the insert part. Its getting the right data to be extracted for formatting that is driving me mad. Maybe I need a clear mind and rested eyes PS: The site id would be called from the file name as it opens the file for the loop.
should be able to do something like this w/ (of course file will be loaded into $filetext via fopen or what ever methodyou wish) $offers = explode("\n\n",$filetext); foreach ($offers as $offer) { $lines = explode("\n",trim($offer)); foreach ($lines as $line) { $error=0; $name = $line[0]; if (preg_match("/:([\S]+) Credit/",$line[1],$match)) { $credits = $match[1]; } else { $error=1; } if (preg_match("/:([\S\s]+)$/",$line[2],$match)) { $cost = $match[1]; } else { $error=1; } if (preg_match("/:([\S\s]+)$/",$line[3],$match)) { $cr_time = $match[1]; } else { $error=1; } if ($error==0){ # SQL INSERT } } } PHP: Dont have time to test it but should be working if you have an issue let me know
so my updated codes is as follow: <?php $filetext = "rb.txt"; $fh = fopen($filetext, 'r+'); $contents = fread($fh, filesize($filetext)); fclose($fh); $offers = explode("\n\n",$contents); foreach ($offers as $offer) { $lines = explode("\n",trim($offer)); $credits=""; $cost=""; $cr_time=""; $cred_flag=0; $cost_flag=0; $cr_flag=0; $name=$lines[0]; foreach ($lines as $line) { if (preg_match("/:([\S]+) Credit/",$line[1],$match)) { $credits = $match[1]; $cred_flag = 1; } if (preg_match("/:([\S\s]+)$/",$line[1],$match)) { $cost = $match[1]; $cost_flag = 1; } if (preg_match("/:([\S\s]+)$/",$line[1],$match)) { $cr_time = $match[1]; $cr_flag = 1; } } echo $name . '<br>'; echo $credits . '<br>'; } ?> PHP: My issue: $name does just fine and returns all the offer titles $credits $cost $cr_time All flops - arent returning anything... is the regex correct? Or am I missing something else?
oops change those to if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) { $credits = $match[1]; $cred_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[1],$match)) { $cost = $match[1]; $cost_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[1],$match)) { $cr_time = $match[1]; $cr_flag = 1; } PHP:
shoot messed up again if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) { $credits = $match[1]; $cred_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[2],$match)) { $cost = $match[1]; $cost_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[3],$match)) { $cr_time = $match[1]; $cr_flag = 1; } PHP:
man im super tired..... give me a minute i messed up this whole thing lol ill use your code you have there and fix it
Oic it was from your changes use this .... there are goign to be somethings missing as im going to bed soon and not adding as i added them in my previous script.... such as trimming the fileinput <?php $filetext = "rb.txt"; $fh = fopen($filetext, 'r+'); $contents = fread($fh, filesize($filetext)); fclose($fh); $offers = explode("\n\n",$contents); foreach ($offers as $offer) { $lines = explode("\n",trim($offer)); $credits=""; $cost=""; $cr_time=""; $cred_flag=0; $cost_flag=0; $cr_flag=0; $name=$lines[0]; if (preg_match("/:([\S]+) Credit/iU",$line[1],$match)) { $credits = $match[1]; $cred_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[2],$match)) { $cost = $match[1]; $cost_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$line[3],$match)) { $cr_time = $match[1]; $cr_flag = 1; } echo $name . '<br>'; echo $credits . '<br>'; } ?> PHP:
Hallent: Thanks for the attempt and I too am tired! The regex is still not working properly its only pulling in the names ok. It is kind of important I get it working within this format, because I do a linux -dump page to get the data as shown above and the format will be the same for all files ran thru this. So if we can get it straight it would be awesome. As is stands right now, your working example and my working example produce the same results - just the offer names Thanks!
LOL i removed a loop you had and left the singular names for the arrays! DOH!!!!! enjoy actually tested.... <?php $filetext = "rb.txt"; $fh = fopen($filetext, 'r+'); $contents = fread($fh, filesize($filetext)); fclose($fh); $offers = explode("\n\n",$contents); foreach ($offers as $offer) { $lines = explode("\n",trim($offer)); $credits=""; $cost=""; $cr_time=""; $cred_flag=0; $cost_flag=0; $cr_flag=0; $name=$lines[0]; if (preg_match("/:([\S]+) Credit/iU",$lines[1],$match)) { $credits = trim($match[1]); $cred_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$lines[2],$match)) { $cost = trim($match[1]); $cost_flag = 1; } if (preg_match("/:([\S\s]+)$/iU",$lines[3],$match)) { $cr_time = trim($match[1]); $cr_flag = 1; } echo $name . '<br>'; echo $credits . '<br>'; echo $cost.'<br>'; echo $cr_time.'<br>'; echo "\n"; } ?> PHP: