An array question???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cem Louis

    An array question???

    Hi,

    I want to get each line of e-mail accounts from my txt file and put
    them in an array after checking the syntax.

    I made a few tries but can't accomplish on that here is my text file
    look like:

    mails.txt

    he@yourdomain.c om
    she@yourdomain. com
    me@yourdomain.c om
    you@yourdomain. com

    And here is my code:

    <?
    $filename = file("mails.txt ");

    foreach($filena me as $line){
    $syntax_ok_emai ls =
    preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);
    if($syntax_ok_e mails){
    $e_mails[] = $syntax_ok_emai ls;
    }
    }
    foreach($e_mail s[] as $value){
    echo $value;
    }
    ?>
  • LJR

    #2
    Re: An array question???

    "Cem Louis" <cemlouis@phrea ker.net> wrote in message
    news:12636200.0 408051342.3ec00 b4@posting.goog le.com...[color=blue]
    > Hi,
    >
    > I want to get each line of e-mail accounts from my txt file and put
    > them in an array after checking the syntax.
    >
    > I made a few tries but can't accomplish on that here is my text file
    > look like:
    >
    > mails.txt
    >
    > he@yourdomain.c om
    > she@yourdomain. com
    > me@yourdomain.c om
    > you@yourdomain. com
    >
    > And here is my code:
    >
    > <?
    > $filename = file("mails.txt ");
    >
    > foreach($filena me as $line){
    > $syntax_ok_emai ls =
    > preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);
    > if($syntax_ok_e mails){
    > $e_mails[] = $syntax_ok_emai ls;
    > }
    > }
    > foreach($e_mail s[] as $value){
    > echo $value;
    > }
    > ?>[/color]

    Your preg_match isn't matching ANY of the email examples you gave and so the
    $e_mails array isn't being loaded with any of addresses. I got a warning
    when running your code, in this case it's because $e_mails isn't even set,
    nevermind an array.

    I'm sure there are hundreds upon hundreds of example checking solutions and
    there are also hundreds of people that will tell you why it's wrong to do it
    this way or that. Here's my interpretation of what you want to do though you
    don't really need to be loading another array with the checked emails, you
    could just output those that come true from the eregi (my example), unless
    you're planning to do something with that array of course.

    Anyhoo, my code:

    <?
    $filename = file("mails.txt ");

    foreach($filena me as $address){
    $address = trim($address);
    if (eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',
    $address)) {
    $e_mails[] = $address;
    }
    }
    foreach ($e_mails as $key => $value) {
    echo $value."<br>";
    }
    ?>

    HTH

    LJR


    Comment

    • LJR

      #3
      Re: An array question???

      "LJR" <i_would_but_ i dont_trust_you@ no-hope.co.uk> wrote in message
      news:4112b53a$0 $41798$65c69314 @mercury.nildra m.net...[color=blue]
      > "Cem Louis" <cemlouis@phrea ker.net> wrote in message
      > news:12636200.0 408051342.3ec00 b4@posting.goog le.com...[color=green]
      >> Hi,
      >>
      >> I want to get each line of e-mail accounts from my txt file and put
      >> them in an array after checking the syntax.
      >>
      >> I made a few tries but can't accomplish on that here is my text file
      >> look like:
      >>
      >> mails.txt
      >>
      >> he@yourdomain.c om
      >> she@yourdomain. com
      >> me@yourdomain.c om
      >> you@yourdomain. com
      >>
      >> And here is my code:
      >>
      >> <?
      >> $filename = file("mails.txt ");
      >>
      >> foreach($filena me as $line){
      >> $syntax_ok_emai ls =
      >> preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);
      >> if($syntax_ok_e mails){
      >> $e_mails[] = $syntax_ok_emai ls;
      >> }
      >> }
      >> foreach($e_mail s[] as $value){
      >> echo $value;
      >> }
      >> ?>[/color]
      >
      > Your preg_match isn't matching ANY of the email examples you gave and so
      > the $e_mails array isn't being loaded with any of addresses. I got a
      > warning when running your code, in this case it's because $e_mails isn't
      > even set, nevermind an array.
      >
      > I'm sure there are hundreds upon hundreds of example checking solutions
      > and there are also hundreds of people that will tell you why it's wrong to
      > do it this way or that. Here's my interpretation of what you want to do
      > though you don't really need to be loading another array with the checked
      > emails, you could just output those that come true from the eregi (my
      > example), unless you're planning to do something with that array of
      > course.
      >
      > Anyhoo, my code:
      >
      > <?
      > $filename = file("mails.txt ");
      >
      > foreach($filena me as $address){
      > $address = trim($address);
      > if (eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$',
      > $address)) {
      > $e_mails[] = $address;
      > }
      > }
      > foreach ($e_mails as $key => $value) {
      > echo $value."<br>";
      > }
      > ?>
      >
      > HTH
      >
      > LJR
      >[/color]

      Oh, in addition to the above, I noticed in your code your last "foreach" was
      the following:
      foreach($e_mail s[] as $value) {
      echo $value;
      }

      It should be:
      foreach($e_mail s as $value) {
      echo $value;
      }

      So, two problems:
      1. preg_match isn't doing its job at all.
      2. Wrong parameter in foreach()

      :)


      Comment

      • Michael Fesser

        #4
        Re: An array question???

        .oO(Cem Louis)
        [color=blue]
        >I want to get each line of e-mail accounts from my txt file and put
        >them in an array after checking the syntax.
        >
        >I made a few tries but can't accomplish on that here is my text file
        >look like: [...][/color]

        There are several issues with your code:

        * Use <?php instead of <?, the latter (short open tags) is less reliable
        and may be disabled on some servers

        * file() returns the file content _including_ line breaks (that's one
        reson why the pattern matching fails)

        * $e_mails is not initialized, the second foreach-loop causes a notice
        if no valid addresses were found

        * There's a missing [ in the pattern in its second part (after the @)

        * The pattern only matches a small subset of valid mail addresses, there
        are much more chars and structures allowed, but maybe it's enough in
        your case

        * You're storing the boolean values instead of the mail addresses in the
        $e_mails array


        Try this:


        $e_mails = array();
        $filename = file('mails.txt ');
        foreach ($filename as $line) {
        $line = trim($line);
        if (preg_match('#^[\w-.]+@([\w-]+\.)+\w{2,4}$#' , $line)) {
        $e_mails[] = $line;
        }
        }


        HTH
        Micha

        Comment

        • Pjotr Wedersteers

          #5
          Re: An array question???

          Cem Louis wrote:[color=blue]
          >
          > he@yourdomain.c om
          > she@yourdomain. com
          > me@yourdomain.c om
          > you@yourdomain. com
          >
          > $syntax_ok_emai ls =
          > preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);[/color]

          I am not a guru on regex, but accoriding to my layman judgement:

          The - in the first [] does not need escaping.
          The second [] misses a [
          I tried your regex with these alterations and preg_match with my email addy
          returns a 1, which is what you want ? I would say you need to store the
          $line itself in a db, not the preg_match result.

          Besides, I wonder if you caver all possible emailaddresses with this, I
          dunno the exact standard for valid email... May have to recheck that

          HTH
          Pjotr


          Comment

          • Ian.H

            #6
            Re: An array question???

            On Sat, 07 Aug 2004 13:17:37 +0200, Pjotr Wedersteers wrote:
            [color=blue]
            > Cem Louis wrote:[color=green]
            >>
            >> he@yourdomain.c om
            >> she@yourdomain. com
            >> me@yourdomain.c om
            >> you@yourdomain. com
            >>
            >> $syntax_ok_emai ls =
            >> preg_match('/^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/', $line);[/color]
            >
            > I am not a guru on regex, but accoriding to my layman judgement:
            >
            > The - in the first [] does not need escaping.
            > The second [] misses a [
            > I tried your regex with these alterations and preg_match with my email addy
            > returns a 1, which is what you want ? I would say you need to store the
            > $line itself in a db, not the preg_match result.
            >
            > Besides, I wonder if you caver all possible emailaddresses with this, I
            > dunno the exact standard for valid email... May have to recheck that
            >
            > HTH
            > Pjotr[/color]


            Nope.. {2,4} for the TLD needs to be {2,6} at least (.museum is a
            legitimate TLD), other issues also occur in the username part (no + or .
            permitted by the above regex etc).

            I found using regex and getmxrr() helps as you can check for MX records
            for a domain name. Again not failsafe obviously, but all helps =)



            Regards,

            Ian

            --
            Ian.H
            digiServ Network
            London, UK


            Comment

            • bruno modulix

              #7
              Re: An array question???

              Michael Fesser a écrit :[color=blue]
              > .oO(Cem Louis)[/color]
              (snip)[color=blue]
              > Try this:
              >
              >
              > $e_mails = array();
              > $filename = file('mails.txt ');
              > foreach ($filename as $line) {[/color]

              And here I would add that $filename is obviously a wrong name for the
              variable... $lines would probably do a better job :

              $lines = files('mails.tx t');
              foreach ($lines as $lines) {
              [color=blue]
              > $line = trim($line);
              > if (preg_match('#^[\w-.]+@([\w-]+\.)+\w{2,4}$#' , $line)) {
              > $e_mails[] = $line;
              > }
              > }
              >[/color]

              Bruno

              Comment

              • Virgil Green

                #8
                Re: An array question???


                "bruno modulix" <onurb@xiludom. gro> wrote in message
                news:411668e5$0 $22905$636a15ce @news.free.fr.. .[color=blue]
                > Michael Fesser a écrit :[color=green]
                > > .oO(Cem Louis)[/color]
                > (snip)[color=green]
                > > Try this:
                > >
                > >
                > > $e_mails = array();
                > > $filename = file('mails.txt ');
                > > foreach ($filename as $line) {[/color]
                >
                > And here I would add that $filename is obviously a wrong name for the
                > variable... $lines would probably do a better job :
                >
                > $lines = files('mails.tx t');
                > foreach ($lines as $lines) {[/color]

                That would cause some grief. Rather "foreach ($lines as $line)" maybe. ;-)

                - Virgil


                Comment

                Working...