Results 1 to 5 of 5

Thread: Text File To A CSV File?

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Text File To A CSV File?

    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?

  2. #2
    Join Date
    Aug 2006
    Posts
    239
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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:
    Code:
    "John Doe", j.doe@aol.com,
    "Agent Mulder", trustno1@aol.com,
    "Somebody else", address@the.net,

  3. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Well, this is the issue: each email address is added like so:

    Code:
    <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>

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Regular Expressions are the best option. Something alogn the lines off
    PHP Code:
    <?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.
    Last edited by blm126; 11-13-2006 at 03:30 AM. Reason: Forgot closing [/PHP]

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Or:
    Code:
    $fn = 'original';
    
    $txt = file_get_contents($fn);
    preg_replace('/<p>(.*?)</p>/gi', '$1,', $txt);
    $f = fopen($fn, "w");
    fwrite($f, $txt);
    fclose($f);
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •