file() and line_num display

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

    #1

    file() and line_num display

    Hello all,

    I have the following file.txt storing these kind of values:

    Name: John Doe
    E-mail: john@doe.com
    Phone: 555 865 8901
    Address: 71 Huntington Ln. (33444) FL - Delray Beach US
    Member ID: 373732510149005
    Registration date: 5/2005
    Access code: 6691
    Comments: Thanks, great community
    =============== =============== ==============
    Name: Bill Gates
    E-mail: billgates@owns. microsoft.com
    Phone: (222) 345-6667
    Address: P.O. Box 36 (73456) OK - Leedey USA
    Member ID: 517805250943762 6
    Registration date: 10/2005
    Access Code: 969
    Comments: Reffered by Josh Portua. Give him credit
    =============== =============== ==============
    Name: ...... and so on

    I want to search the file.txt after 3 predefined Member ID prefixes:
    $prefix1="51780 ";
    $prefix2="37373 ";
    $prefix3="72215 ";
    show the Member IDs that match this rule and also display for the found
    matching Member IDs their E-mail and Name.

    So the search on the part of file.txt shown at the start will result:
    Member ID: 517805250943762 6 E-mail: billgates@owns. microsoft.com Name:
    Bill Gates

    I have built something like this so far:

    <?php
    $prefix1="51780 ";
    $prefix2="37373 ";
    $prefix3="72215 ";
    $userfile = "users.txt" ;
    $fc=file($userf ile);
    foreach($fc as $line_num => $line)
    {
    if (strstr($line,$ prefix1))
    echo "Found one match at {$line_num} $line<br>";
    if (strstr($line,$ prefix2))
    echo "Found one match at {$line_num} $line<br>";
    if (strstr($line,$ prefix3))
    echo "Found one match at {$line_num} $line<br>";
    }
    ?>

    What I can't do is showing the emails and names for the found member
    ids. Please, can you give me any suggestions? I'm completly blank.

    Thank you for your time.

    Regards,
    Mark

  • Jerry Stuckle

    #2
    Re: file() and line_num display

    Mark wrote:[color=blue]
    > Hello all,
    >
    > I have the following file.txt storing these kind of values:
    >
    > Name: John Doe
    > E-mail: john@doe.com
    > Phone: 555 865 8901
    > Address: 71 Huntington Ln. (33444) FL - Delray Beach US
    > Member ID: 373732510149005
    > Registration date: 5/2005
    > Access code: 6691
    > Comments: Thanks, great community
    > =============== =============== ==============
    > Name: Bill Gates
    > E-mail: billgates@owns. microsoft.com
    > Phone: (222) 345-6667
    > Address: P.O. Box 36 (73456) OK - Leedey USA
    > Member ID: 517805250943762 6
    > Registration date: 10/2005
    > Access Code: 969
    > Comments: Reffered by Josh Portua. Give him credit
    > =============== =============== ==============
    > Name: ...... and so on
    >
    > I want to search the file.txt after 3 predefined Member ID prefixes:
    > $prefix1="51780 ";
    > $prefix2="37373 ";
    > $prefix3="72215 ";
    > show the Member IDs that match this rule and also display for the found
    > matching Member IDs their E-mail and Name.
    >
    > So the search on the part of file.txt shown at the start will result:
    > Member ID: 517805250943762 6 E-mail: billgates@owns. microsoft.com Name:
    > Bill Gates
    >
    > I have built something like this so far:
    >
    > <?php
    > $prefix1="51780 ";
    > $prefix2="37373 ";
    > $prefix3="72215 ";
    > $userfile = "users.txt" ;
    > $fc=file($userf ile);
    > foreach($fc as $line_num => $line)
    > {
    > if (strstr($line,$ prefix1))
    > echo "Found one match at {$line_num} $line<br>";
    > if (strstr($line,$ prefix2))
    > echo "Found one match at {$line_num} $line<br>";
    > if (strstr($line,$ prefix3))
    > echo "Found one match at {$line_num} $line<br>";
    > }
    > ?>
    >
    > What I can't do is showing the emails and names for the found member
    > ids. Please, can you give me any suggestions? I'm completly blank.
    >
    > Thank you for your time.
    >
    > Regards,
    > Mark
    >[/color]


    Mark,

    I'd use a database such as MySql or Postgres. They are ideally suited for this
    kind of operation. And it's not that hard to program for them.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Andy Jeffries

      #3
      Re: file() and line_num display

      On Tue, 28 Mar 2006 18:57:53 -0800, Mark wrote:
      [color=blue]
      > Hello all,
      >
      > I have the following file.txt storing these kind of values:
      >
      > Name: John Doe
      > E-mail: john@doe.com
      > Phone: 555 865 8901
      > Address: 71 Huntington Ln. (33444) FL - Delray Beach US Member ID:
      > 373732510149005
      > Registration date: 5/2005
      > Access code: 6691
      > Comments: Thanks, great community
      > =============== =============== ============== Name: Bill Gates
      > E-mail: billgates@owns. microsoft.com
      > Phone: (222) 345-6667
      > Address: P.O. Box 36 (73456) OK - Leedey USA Member ID: 517805250943762 6
      > Registration date: 10/2005
      > Access Code: 969
      > Comments: Reffered by Josh Portua. Give him credit
      > =============== =============== ============== Name: ...... and so on
      >
      > I want to search the file.txt after 3 predefined Member ID prefixes:
      > $prefix1="51780 ";
      > $prefix2="37373 ";
      > $prefix3="72215 ";
      > show the Member IDs that match this rule and also display for the found
      > matching Member IDs their E-mail and Name.[/color]

      OK, new script follows:

      <?php
      $prefix1="51780 ";
      $prefix2="37373 ";
      $prefix3="72215 ";
      $userfile = "users.txt" ;
      $fc=file($userf ile);
      foreach($fc as $line_num => $line)
      {
      $line = trim($line); // Line will have newline characters
      if (ereg("^E-mail: (.+)", $line, $matches)) {
      $email = $matches[1];
      }
      if (ereg("^Name: (.+)", $line, $matches)) {
      $name = $matches[1];
      }

      if (strstr($line,$ prefix1)) {
      print "Found ID $prefix1\nName: $name\nEmail: $email\n\n";
      }
      elseif (strstr($line,$ prefix2)) {
      print "Found ID $prefix2\nName: $name\nEmail: $email\n\n";
      }
      elseif (strstr($line,$ prefix3)) {
      print "Found ID $prefix2\nName: $name\nEmail: $email\n\n";
      }
      }
      ?>

      This assumes, as per your data that the Name and Email address for a given
      member appears before their membership ID.

      If not, you'd need to store the ID in the same was as the ereg statements
      for name and email and react on reaching the line with all the equals
      statements, then see if the ID matches.

      Cheers,



      --
      Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
      http://www.gphpedit.org | PHP editor for Gnome 2
      http://www.andyjeffries.co.uk | Personal site and photos

      Comment

      • Andy Jeffries

        #4
        Re: file() and line_num display

        On Tue, 28 Mar 2006 22:34:08 -0500, Jerry Stuckle wrote:[color=blue]
        > I'd use a database such as MySql or Postgres. They are ideally suited for
        > this kind of operation. And it's not that hard to program for them.[/color]

        I'd agree if he is the one inserting them in to a text file (he may be
        being given it by another party).

        If he only has the text file, he'd have to parse it either way to insert
        it into a DB or to just display it - so he might as well just display it.

        Cheers,


        Andy

        --
        Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
        http://www.gphpedit.org | PHP editor for Gnome 2
        http://www.andyjeffries.co.uk | Personal site and photos

        Comment

        • Jerry Stuckle

          #5
          Re: file() and line_num display

          Andy Jeffries wrote:[color=blue]
          > On Tue, 28 Mar 2006 22:34:08 -0500, Jerry Stuckle wrote:
          >[color=green]
          >>I'd use a database such as MySql or Postgres. They are ideally suited for
          >>this kind of operation. And it's not that hard to program for them.[/color]
          >
          >
          > I'd agree if he is the one inserting them in to a text file (he may be
          > being given it by another party).
          >
          > If he only has the text file, he'd have to parse it either way to insert
          > it into a DB or to just display it - so he might as well just display it.
          >
          > Cheers,
          >
          >
          > Andy
          >[/color]

          Yes, he would have to parse it *once* to place all the information in the
          database. As a flat file he would have to parse it every time he wants anything
          from the database. And if the file format ever changes, he'll have to redo the
          code every place he needs to access it.

          Databases make your life so much simpler - even if you're not inserting into a
          file. Even with static data I almost always put the information in a database;
          it's just so much easier to access - and you don't run into problems like this.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Andy Jeffries

            #6
            Re: file() and line_num display

            On Wed, 29 Mar 2006 05:36:04 -0500, Jerry Stuckle wrote:[color=blue][color=green][color=darkred]
            >>>I'd use a database such as MySql or Postgres. They are ideally suited
            >>>for this kind of operation. And it's not that hard to program for them.[/color]
            >>
            >> I'd agree if he is the one inserting them in to a text file (he may be
            >> being given it by another party).
            >>
            >> If he only has the text file, he'd have to parse it either way to insert
            >> it into a DB or to just display it - so he might as well just display
            >> it.[/color]
            >
            > Yes, he would have to parse it *once* to place all the information in the
            > database. As a flat file he would have to parse it every time he wants
            > anything from the database. And if the file format ever changes, he'll
            > have to redo the code every place he needs to access it.[/color]

            It depends what he's using the data for. Maybe this is code to parse it
            put in to a database? I know at the moment he only wants to print it, but
            maybe he's just taking it one step at a time.

            To be honest, I think we actually have the same point of view on this, I
            completely agree with you - I was just trying to explain (more for the
            OP's benefit as I know you're a knowledgeable guy) that a database is a
            great end-goal, but if it's for a one-off run it might be easier to just
            display the information rather than add an intermediate step.

            Your answer to "how do I display these fields" was "use a database"
            without the intermediate step of how to get his data from the text file he
            has in to the empty database he could make.
            [color=blue]
            > Databases make your life so much simpler - even if you're not inserting
            > into a file. Even with static data I almost always put the information
            > in a database; it's just so much easier to access - and you don't run
            > into problems like this.[/color]

            Again though, while I generally agree - it depends on the purpose.
            Without more information from the OP it's difficult for you or I to advise
            whether putting it in a database is the best course of action.

            For example, this may be a much simplified version of what he wants and it
            may just be that his boss has said "here's a one-off dump of the CRM
            database, can you get me the details for these users". He's
            therefore writing a script to do it (wise man, the boss may ask the same
            thing next month) but creating a database would be overkill as it won't be
            accessed until possibly next month and by then the data would need
            re-importing anyway.

            But as I said, without knowing we're both just guessing :-)

            Cheers,


            Andy



            --
            Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
            http://www.gphpedit.org | PHP editor for Gnome 2
            http://www.andyjeffries.co.uk | Personal site and photos

            Comment

            • Mark

              #7
              Re: file() and line_num display

              I would like to give a heartly "Thank You!" to Andy Jeffries for
              helping me out on this. The script was just what I needed. And you made
              a really good point in your replies. I could have used a database but
              that was just too much work for something that won't be used intense.
              Those dumps appear from time to time and it's no point in having them
              stored in a database just for one time use.

              Again, Thank you Andy Jeffries for all your help, for your flawless
              script and for giving me priceless directions. I was so blanked-out
              before and couldn't have imagined a way to solve this.

              Best regards,
              Mark

              Comment

              Working...