SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular expression help

    Hi,

    I have a text file with over 5200 lines of e-mail addresses. They currently look like this:

    (Joe Blow) <joeblow@comcast.net>,

    I need to remove the (Joe Blow) as well as the two angle brackets, the space and the ending comma. It should look like this:

    joeblow@comcast.net

    I can code the PHP part, but I need help formulating the regular expression.

    Thank you in advance,
    Mark

  2. #2
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    my first idea is this:
    PHP Code:
    <?php // someone gets angry me using <? :))
    $myfile fopen ("emails.txt""r");
    $myfile2 fopen ("newemails.txt""w");
    while (!
    feof($myfile)) { 
    $emaildata fgets ($myfile,4096);
    $emaildata explode (")"$emaildata);
    $emaildata[1] = eregi_replace("<","",$emaildata[1]);
    $emaildata[1] = eregi_replace(">","",$emaildata[1]);
    $emaildata[1] = eregi_replace(" ","",$emaildata[1]);

    fwrite($myfile2,$emaildata[1]."\n");

    }
    fclose($myfile);
    fclose($myfile2);
    ?>
    of course there are more was (more little codes) but i can't remember if i remember i write it here...

    gooood work

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok here is another code with strstr:

    PHP Code:
    <?php
    $myfile 
    fopen ("emails.txt""r"); 
    $myfile2 fopen ("newemails.txt""w"); 
    while (!
    feof($myfile)) { 
    $emaildata fgets ($myfile,4096);
    $emaildata strstr ($emaildata,")");
    $emaildata trim($emaildata);
    $emaildata eregi_replace("<","",$emaildata); 
    $emaildata eregi_replace(">","",$emaildata); 
    fwrite($myfile2,$emaildata."\n");
    }
    fclose($myfile); 
    fclose($myfile2);
    ?>
    do not use my codes for spamming )

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the quick reply.

    It almost worked What happened is that those lines that did not have a real name in parenthesis wound up being deleted all together. Here is a larger sample from my e-mail list:

    <deleted e-mail list>

    As I said in my original e-mail, I would like to get rid of the (real name) as well as the angle brackets and comma.

    Thanks,
    Mark

    P.S. This is for a client of mine who is moving his mailing list and I need to clean up the data first.

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok let's think about some different ways, but don't you say that there was no name users there, here is edited for version 1:

    PHP Code:
    <?php // someone gets angry me using <? :)) 
    $myfile fopen ("emails.txt""r"); 
    $myfile2 fopen ("newemails.txt""w"); 
    while (!
    feof($myfile)) { 
    $emaildata fgets ($myfile,4096); 
    if (
    ereg(")"$emaildata)) { // if there is a name
    $emaildata explode (")"$emaildata);
    $emaildata[1] = eregi_replace("<","",$emaildata[1]); 
    $emaildata[1] = eregi_replace(">","",$emaildata[1]); 
    $emaildata[1] = eregi_replace(" ","",$emaildata[1]); 
    $emaildata[1] = eregi_replace(",","",$emaildata[1]);
    fwrite($myfile2,$emaildata[1]."\n"); 
    } else { 
    // else there isnt a name
    $emaildata eregi_replace(",","",$emaildata); 
    $emaildata eregi_replace("<","",$emaildata); 
    $emaildata eregi_replace(">","",$emaildata);
    fwrite($myfile2,$emaildata."\n"); 
    }

    fclose($myfile); 
    fclose($myfile2); 
    ?>
    I hope this works dont forget to backup your files )

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, that worked!

    Thank you for your assistance.

    Mark

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

        $emails 
    file_get_contents './emaillist.txt' );

        
    preg_match_all '#<(.+?)>#'$emails$matches );

        echo 
    '<pre>'print_r $matches[1] ), '</pre>';

    ?>
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Zealot
    Join Date
    Nov 2004
    Location
    Ankara/Turkey
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i mean this with more little codes

  9. #9
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    PHP Code:
    <?php

        $emails 
    file_get_contents './emaillist.txt' );

        
    preg_match_all '#<(.+?)>#'$emails$matches );

        echo 
    '<pre>'print_r $matches[1] ), '</pre>';

    ?>
    Sean
    bingo! rarely should you need to use multiple regular expressions for simple standardized formats. the only times are pretty much for loops and recursivity. with regard to the examples above that only used ereg/preg to match literal strings, the non-regex php string functions are much faster for this


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
  •