Log in

View Full Version : Text File To A CSV File?



alexjewell
11-12-2006, 02:16 AM
I did a website for the company I work for that when someone visits, for legal reasons, they are asked to submit their email address to view certain pages - that way we keep a general idea of who's visiting. The email addresses are written onto a flat text file, via simple PHP, and the text file is then imported into an admin page so that we can view these email addresses.

Now, they want me to make a CSV file so that they can send out a mass email to everyone who visited the site...
This means somehow converting this text file to a csv file...

If I had originally used MySQL, I know this wouldn't have been much of a problem. However, because I went with a simple text file, not thinking we'd need MySQL, I'm at a loss of what to do?

How would I go about this?

ItsMeOnly
11-12-2006, 09:48 AM
CSV file is a text file, where fields are grouped using separators, like tabs and commas (the name says it all: comma separated values)...

Example:


"John Doe", j.doe@aol.com,
"Agent Mulder", trustno1@aol.com,
"Somebody else", address@the.net,

alexjewell
11-13-2006, 12:58 AM
Well, this is the issue: each email address is added like so:


<p>August 10, 2006 -- alexjewell@sbcglobal.net</p>

How would I, without doing it manually, change it to CSV? There are A LOT of these...

I mean, there's no seperater...each one is just a <p>

blm126
11-13-2006, 03:30 AM
Regular Expressions are the best option. Something alogn the lines off


<?php
$file = file_get_contents("orignal.txt");
preg_match_all("/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/",$file,$email);
array_shift($email);
$out = implode(',',$emal);
file_put_contents('new.txt',$out);
?>

not that my knowledge of regular expressions is not very good. And this is intested....so you may have to modify this, but it should at least get you going in the right direction.

Twey
11-13-2006, 05:46 PM
Or:
$fn = 'original';

$txt = file_get_contents($fn);
preg_replace('/<p>(.*?)</p>/gi', '$1,', $txt);
$f = fopen($fn, "w");
fwrite($f, $txt);
fclose($f);