Need help with exploding array

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

    Need help with exploding array

    Here's my problem. I am reading from a text file using this:

    if (!file_exists($ file))
    {
    echo "Don't exist\n";
    return false;
    }

    $fd = @fopen($file, 'r');
    if (!is_resource($ fd))
    {
    echo "Error reading\n";
    return false;
    }

    And it works fine. The data that will be read will always be in the text
    file in the form of

    one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu

    Now I want to explode that array that was read so I use this:

    $arrayData = explode("-", fgets($fd, 4096));

    print "first=$arrayDa ta[0]\n";
    print "second=$arrayD ata[1]\n";
    print "third=$arrayDa ta[2]\n";
    print "fourth=$arrayD ata[3]\n";

    which will give me

    first=one second=two third=three fourth=alpha beta kappa delta epsilon
    13.5pp 29.95eu

    But I need to access the last two items (13.5pp and 29.95eu) of this
    array to split them off as well so that the final output I need would be
    like this:

    first=one second=two third=three fourth=alpha beta kappa delta
    epsilon.txt numpages=13.5pp cost=29.95eu

    How can I do that? Do I need to further explode $arrayData[3] using a
    space delimiter? Is there a way to grab the last two fields only because
    the length of the 'alpha beta kappa delta epsilon' will not always be
    the same but the numpages and cost will always be the last two elements
    in the array. Any help would be appreciated.

  • Erwin Moller

    #2
    Re: Need help with exploding array

    Nathan Rose wrote:
    [color=blue]
    > Here's my problem. I am reading from a text file using this:
    >
    > if (!file_exists($ file))
    > {
    > echo "Don't exist\n";
    > return false;
    > }
    >
    > $fd = @fopen($file, 'r');
    > if (!is_resource($ fd))
    > {
    > echo "Error reading\n";
    > return false;
    > }
    >
    > And it works fine. The data that will be read will always be in the text
    > file in the form of
    >
    > one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
    >
    > Now I want to explode that array that was read so I use this:
    >
    > $arrayData = explode("-", fgets($fd, 4096));
    >
    > print "first=$arrayDa ta[0]\n";
    > print "second=$arrayD ata[1]\n";
    > print "third=$arrayDa ta[2]\n";
    > print "fourth=$arrayD ata[3]\n";
    >
    > which will give me
    >
    > first=one second=two third=three fourth=alpha beta kappa delta epsilon
    > 13.5pp 29.95eu
    >
    > But I need to access the last two items (13.5pp and 29.95eu) of this
    > array to split them off as well so that the final output I need would be
    > like this:
    >
    > first=one second=two third=three fourth=alpha beta kappa delta
    > epsilon.txt numpages=13.5pp cost=29.95eu
    >
    > How can I do that? Do I need to further explode $arrayData[3] using a
    > space delimiter?[/color]

    Indeed.
    Sounds logical.

    Is there a way to grab the last two fields only because[color=blue]
    > the length of the 'alpha beta kappa delta epsilon' will not always be
    > the same but the numpages and cost will always be the last two elements
    > in the array. Any help would be appreciated.[/color]

    So use the last part of the first explode to explode again.
    Check the number of elements you get back, take the last 2.

    Regards,
    Erwin Moller

    Comment

    • Nathan Rose

      #3
      Re: Need help with exploding array

      Erwin Moller wrote:[color=blue]
      > Nathan Rose wrote:
      >
      >[color=green]
      >>Here's my problem. I am reading from a text file using this:
      >>
      >> if (!file_exists($ file))
      >> {
      >> echo "Don't exist\n";
      >> return false;
      >> }
      >>
      >> $fd = @fopen($file, 'r');
      >> if (!is_resource($ fd))
      >> {
      >> echo "Error reading\n";
      >> return false;
      >> }
      >>
      >>And it works fine. The data that will be read will always be in the text
      >>file in the form of
      >>
      >>one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
      >>
      >>Now I want to explode that array that was read so I use this:
      >>
      >>$arrayData = explode("-", fgets($fd, 4096));
      >>
      >>print "first=$arrayDa ta[0]\n";
      >>print "second=$arrayD ata[1]\n";
      >>print "third=$arrayDa ta[2]\n";
      >>print "fourth=$arrayD ata[3]\n";
      >>
      >>which will give me
      >>
      >>first=one second=two third=three fourth=alpha beta kappa delta epsilon
      >>13.5pp 29.95eu
      >>
      >>But I need to access the last two items (13.5pp and 29.95eu) of this
      >>array to split them off as well so that the final output I need would be
      >>like this:
      >>
      >>first=one second=two third=three fourth=alpha beta kappa delta
      >>epsilon.txt numpages=13.5pp cost=29.95eu
      >>
      >>How can I do that? Do I need to further explode $arrayData[3] using a
      >>space delimiter?[/color]
      >
      >
      > Indeed.
      > Sounds logical.
      >
      > Is there a way to grab the last two fields only because
      >[color=green]
      >>the length of the 'alpha beta kappa delta epsilon' will not always be
      >>the same but the numpages and cost will always be the last two elements
      >>in the array. Any help would be appreciated.[/color]
      >
      >
      > So use the last part of the first explode to explode again.
      > Check the number of elements you get back, take the last 2.
      >[/color]


      Well at least it seems I am on the right track. :)

      I had thought I remembered at one time seeing a solution about reversing
      an array so that the last element was first, second to last was second,
      etc. It just seems to be easier to explode the $arrayData[3], then
      reverse it (into say $revArrayData) and take the reversed order as
      $revArrayData[1] and $revArrayData[2].

      Was I hallucinating about this or does something like it really exist?

      Comment

      • Michael Austin

        #4
        Re: Need help with exploding array

        Nathan Rose wrote:
        [color=blue]
        > Erwin Moller wrote:
        >[color=green]
        >> Nathan Rose wrote:
        >>
        >>[color=darkred]
        >>> Here's my problem. I am reading from a text file using this:
        >>>
        >>> if (!file_exists($ file))
        >>> {
        >>> echo "Don't exist\n";
        >>> return false;
        >>> }
        >>>
        >>> $fd = @fopen($file, 'r');
        >>> if (!is_resource($ fd))
        >>> {
        >>> echo "Error reading\n";
        >>> return false;
        >>> }
        >>>
        >>> And it works fine. The data that will be read will always be in the text
        >>> file in the form of
        >>>
        >>> one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
        >>>
        >>> Now I want to explode that array that was read so I use this:
        >>>
        >>> $arrayData = explode("-", fgets($fd, 4096));
        >>>
        >>> print "first=$arrayDa ta[0]\n";
        >>> print "second=$arrayD ata[1]\n";
        >>> print "third=$arrayDa ta[2]\n";
        >>> print "fourth=$arrayD ata[3]\n";
        >>>
        >>> which will give me
        >>>
        >>> first=one second=two third=three fourth=alpha beta kappa delta epsilon
        >>> 13.5pp 29.95eu
        >>>
        >>> But I need to access the last two items (13.5pp and 29.95eu) of this
        >>> array to split them off as well so that the final output I need would be
        >>> like this:
        >>>
        >>> first=one second=two third=three fourth=alpha beta kappa delta
        >>> epsilon.txt numpages=13.5pp cost=29.95eu
        >>>
        >>> How can I do that? Do I need to further explode $arrayData[3] using a
        >>> space delimiter?[/color]
        >>
        >>
        >>
        >> Indeed.
        >> Sounds logical.
        >>
        >> Is there a way to grab the last two fields only because
        >>[color=darkred]
        >>> the length of the 'alpha beta kappa delta epsilon' will not always be
        >>> the same but the numpages and cost will always be the last two elements
        >>> in the array. Any help would be appreciated.[/color]
        >>
        >>
        >>
        >> So use the last part of the first explode to explode again.
        >> Check the number of elements you get back, take the last 2.
        >>[/color]
        >
        >
        > Well at least it seems I am on the right track. :)
        >
        > I had thought I remembered at one time seeing a solution about reversing
        > an array so that the last element was first, second to last was second,
        > etc. It just seems to be easier to explode the $arrayData[3], then
        > reverse it (into say $revArrayData) and take the reversed order as
        > $revArrayData[1] and $revArrayData[2].
        >
        > Was I hallucinating about this or does something like it really exist?
        >[/color]

        Here: http://www.php.net/ bookmark it, use it. on the upper right there are
        two boxes: search for[ ] in the [ ][v]

        Enter: "reverse array" in the first box and select "functions list" or "whole
        site" in the second -- see what you find... Again. Bookmark it. Use it. you
        will be amazed at what you find...

        --
        Michael Austin.

        Comment

        • Nathan Rose

          #5
          Re: Need help with exploding array

          Michael Austin wrote:
          [color=blue][color=green][color=darkred]
          >>> Nathan Rose wrote:
          >>>
          >>>
          >>>> Here's my problem. I am reading from a text file using this:
          >>>>
          >>>> if (!file_exists($ file))
          >>>> {
          >>>> echo "Don't exist\n";
          >>>> return false;
          >>>> }
          >>>>
          >>>> $fd = @fopen($file, 'r');
          >>>> if (!is_resource($ fd))
          >>>> {
          >>>> echo "Error reading\n";
          >>>> return false;
          >>>> }
          >>>>
          >>>> And it works fine. The data that will be read will always be in the
          >>>> text
          >>>> file in the form of
          >>>>
          >>>> one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
          >>>>
          >>>> Now I want to explode that array that was read so I use this:
          >>>>
          >>>> $arrayData = explode("-", fgets($fd, 4096));
          >>>>
          >>>> print "first=$arrayDa ta[0]\n";
          >>>> print "second=$arrayD ata[1]\n";
          >>>> print "third=$arrayDa ta[2]\n";
          >>>> print "fourth=$arrayD ata[3]\n";
          >>>>
          >>>> which will give me
          >>>>
          >>>> first=one second=two third=three fourth=alpha beta kappa delta epsilon
          >>>> 13.5pp 29.95eu
          >>>>
          >>>> But I need to access the last two items (13.5pp and 29.95eu) of this
          >>>> array to split them off as well so that the final output I need
          >>>> would be
          >>>> like this:
          >>>>
          >>>> first=one second=two third=three fourth=alpha beta kappa delta
          >>>> epsilon.txt numpages=13.5pp cost=29.95eu
          >>>>
          >>>
          >>>
          >>>> the length of the 'alpha beta kappa delta epsilon' will not always be
          >>>> the same but the numpages and cost will always be the last two elements
          >>>> in the array. Any help would be appreciated.
          >>>[/color]
          >>
          >>
          >>
          >> I had thought I remembered at one time seeing a solution about
          >> reversing an array so that the last element was first, second to last
          >> was second, etc. It just seems to be easier to explode the
          >> $arrayData[3], then reverse it (into say $revArrayData) and take the
          >> reversed order as $revArrayData[1] and $revArrayData[2].
          >>
          >> Was I hallucinating about this or does something like it really exist?
          >>[/color]
          >
          > Here: http://www.php.net/ bookmark it, use it. on the upper right
          > there are two boxes: search for[ ] in the [ ][v]
          >
          > Enter: "reverse array" in the first box and select "functions list" or
          > "whole site" in the second -- see what you find... Again. Bookmark it.
          > Use it. you will be amazed at what you find...
          >[/color]

          Thank you. I have gotten the reserse_array to work as I need. But I have
          one more question that I can't find an answer to by searching.

          How can I print the $arrayData[3] and leave off the last two items
          (13.5pp 29.95eu) so that the array would print as:
          fourth=alpha beta kappa delta epsilon
          instead of
          fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu

          Comment

          • Erwin Moller

            #6
            Re: Need help with exploding array

            Nathan Rose wrote:
            [color=blue]
            > Michael Austin wrote:
            >[color=green][color=darkred]
            >>>> Nathan Rose wrote:
            >>>>
            >>>>
            >>>>> Here's my problem. I am reading from a text file using this:
            >>>>>
            >>>>> if (!file_exists($ file))
            >>>>> {
            >>>>> echo "Don't exist\n";
            >>>>> return false;
            >>>>> }
            >>>>>
            >>>>> $fd = @fopen($file, 'r');
            >>>>> if (!is_resource($ fd))
            >>>>> {
            >>>>> echo "Error reading\n";
            >>>>> return false;
            >>>>> }
            >>>>>
            >>>>> And it works fine. The data that will be read will always be in the
            >>>>> text
            >>>>> file in the form of
            >>>>>
            >>>>> one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
            >>>>>
            >>>>> Now I want to explode that array that was read so I use this:
            >>>>>
            >>>>> $arrayData = explode("-", fgets($fd, 4096));
            >>>>>
            >>>>> print "first=$arrayDa ta[0]\n";
            >>>>> print "second=$arrayD ata[1]\n";
            >>>>> print "third=$arrayDa ta[2]\n";
            >>>>> print "fourth=$arrayD ata[3]\n";
            >>>>>
            >>>>> which will give me
            >>>>>
            >>>>> first=one second=two third=three fourth=alpha beta kappa delta epsilon
            >>>>> 13.5pp 29.95eu
            >>>>>
            >>>>> But I need to access the last two items (13.5pp and 29.95eu) of this
            >>>>> array to split them off as well so that the final output I need
            >>>>> would be
            >>>>> like this:
            >>>>>
            >>>>> first=one second=two third=three fourth=alpha beta kappa delta
            >>>>> epsilon.txt numpages=13.5pp cost=29.95eu
            >>>>>
            >>>>
            >>>>
            >>>>> the length of the 'alpha beta kappa delta epsilon' will not always be
            >>>>> the same but the numpages and cost will always be the last two
            >>>>> elements in the array. Any help would be appreciated.
            >>>>
            >>>
            >>>
            >>>
            >>> I had thought I remembered at one time seeing a solution about
            >>> reversing an array so that the last element was first, second to last
            >>> was second, etc. It just seems to be easier to explode the
            >>> $arrayData[3], then reverse it (into say $revArrayData) and take the
            >>> reversed order as $revArrayData[1] and $revArrayData[2].
            >>>
            >>> Was I hallucinating about this or does something like it really exist?
            >>>[/color]
            >>
            >> Here: http://www.php.net/ bookmark it, use it. on the upper right
            >> there are two boxes: search for[ ] in the [ ][v]
            >>
            >> Enter: "reverse array" in the first box and select "functions list" or
            >> "whole site" in the second -- see what you find... Again. Bookmark it.
            >> Use it. you will be amazed at what you find...
            >>[/color]
            >
            > Thank you. I have gotten the reserse_array to work as I need. But I have
            > one more question that I can't find an answer to by searching.
            >
            > How can I print the $arrayData[3] and leave off the last two items
            > (13.5pp 29.95eu) so that the array would print as:
            > fourth=alpha beta kappa delta epsilon
            > instead of
            > fourth=alpha beta kappa delta epsilon.txt 13.5pp 29.95eu[/color]


            Use count($somearra y) to get back the number of elements.
            example:
            $arr[0]="hai";
            $arr[1]="hello";
            $arr[3]="hoe do ya do?";

            count($arr) will return 3.

            so... you should be able to figure it our yourself how to strip the last 2
            elements after your explode.

            Regards,
            Erwin Moller

            Comment

            • Nathan Rose

              #7
              Re: Need help with exploding array

              Erwin Moller wrote:[color=blue]
              > Nathan Rose wrote:
              >
              >[color=green]
              >>Michael Austin wrote:
              >>
              >>[color=darkred]
              >>>>>Nathan Rose wrote:
              >>>>>
              >>>>>
              >>>>>
              >>>>>>Here's my problem. I am reading from a text file using this:
              >>>>>>
              >>>>>> if (!file_exists($ file))
              >>>>>> {
              >>>>>> echo "Don't exist\n";
              >>>>>> return false;
              >>>>>> }
              >>>>>>
              >>>>>> $fd = @fopen($file, 'r');
              >>>>>> if (!is_resource($ fd))
              >>>>>> {
              >>>>>> echo "Error reading\n";
              >>>>>> return false;
              >>>>>> }
              >>>>>>
              >>>>>>And it works fine. The data that will be read will always be in the
              >>>>>>text
              >>>>>>file in the form of
              >>>>>>
              >>>>>>one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
              >>>>>>
              >>>>>>Now I want to explode that array that was read so I use this:
              >>>>>>
              >>>>>>$arrayDat a = explode("-", fgets($fd, 4096));
              >>>>>>
              >>>>>>print "first=$arrayDa ta[0]\n";
              >>>>>>print "second=$arrayD ata[1]\n";
              >>>>>>print "third=$arrayDa ta[2]\n";
              >>>>>>print "fourth=$arrayD ata[3]\n";
              >>>>>>
              >>>>>>which will give me
              >>>>>>
              >>>>>>first=o ne second=two third=three fourth=alpha beta kappa delta epsilon
              >>>>>>13.5pp 29.95eu
              >>>>>>
              >>>>>>But I need to access the last two items (13.5pp and 29.95eu) of this
              >>>>>>array to split them off as well so that the final output I need
              >>>>>>would be
              >>>>>>like this:
              >>>>>>
              >>>>>>first=o ne second=two third=three fourth=alpha beta kappa delta
              >>>>>>epsilon.t xt numpages=13.5pp cost=29.95eu
              >>>>>>
              >>>>>
              >>>>>
              >>>>>>the length of the 'alpha beta kappa delta epsilon' will not always be
              >>>>>>the same but the numpages and cost will always be the last two
              >>>>>>element s in the array. Any help would be appreciated.
              >>>>>
              >>>>
              >>>>
              >>>>I had thought I remembered at one time seeing a solution about
              >>>>reversing an array so that the last element was first, second to last
              >>>>was second, etc. It just seems to be easier to explode the
              >>>>$arrayDat a[3], then reverse it (into say $revArrayData) and take the
              >>>>reversed order as $revArrayData[1] and $revArrayData[2].
              >>>>
              >>>>Was I hallucinating about this or does something like it really exist?
              >>>>
              >>>
              >>>Here: http://www.php.net/ bookmark it, use it. on the upper right
              >>>there are two boxes: search for[ ] in the [ ][v]
              >>>
              >>>Enter: "reverse array" in the first box and select "functions list" or
              >>>"whole site" in the second -- see what you find... Again. Bookmark it.
              >>>Use it. you will be amazed at what you find...
              >>>[/color]
              >>
              >>Thank you. I have gotten the reserse_array to work as I need. But I have
              >>one more question that I can't find an answer to by searching.
              >>
              >>How can I print the $arrayData[3] and leave off the last two items
              >>(13.5pp 29.95eu) so that the array would print as:
              >>fourth=alph a beta kappa delta epsilon
              >>instead of
              >>fourth=alph a beta kappa delta epsilon.txt 13.5pp 29.95eu[/color]
              >
              >
              >
              > Use count($somearra y) to get back the number of elements.
              > example:
              > $arr[0]="hai";
              > $arr[1]="hello";
              > $arr[3]="hoe do ya do?";
              >
              > count($arr) will return 3.
              >
              > so... you should be able to figure it our yourself how to strip the last 2
              > elements after your explode.
              >[/color]

              Thank you. I understand about finding out the count of the array and if
              every line that was being read into the array was the same length, I
              could figure out how to leave of the last two elements easily. But as I
              stated earlier, the length of the 'alpha beta kappa delta epsilon' will
              not always be the same but the numpages and cost will always be the last
              two elements in the array.

              So I'm back to the question of how can I print the $arrayData[3] data
              and leave off the last two elements when the length of the $arrayData[3]
              data will vary from line to line?

              Comment

              • Erwin Moller

                #8
                Re: Need help with exploding array

                Nathan Rose wrote:
                [color=blue]
                > Erwin Moller wrote:[color=green]
                >> Nathan Rose wrote:
                >>
                >>[color=darkred]
                >>>Michael Austin wrote:
                >>>
                >>>
                >>>>>>Nathan Rose wrote:
                >>>>>>
                >>>>>>
                >>>>>>
                >>>>>>>Here's my problem. I am reading from a text file using this:
                >>>>>>>
                >>>>>>> if (!file_exists($ file))
                >>>>>>> {
                >>>>>>> echo "Don't exist\n";
                >>>>>>> return false;
                >>>>>>> }
                >>>>>>>
                >>>>>>> $fd = @fopen($file, 'r');
                >>>>>>> if (!is_resource($ fd))
                >>>>>>> {
                >>>>>>> echo "Error reading\n";
                >>>>>>> return false;
                >>>>>>> }
                >>>>>>>
                >>>>>>>And it works fine. The data that will be read will always be in the
                >>>>>>>text
                >>>>>>>file in the form of
                >>>>>>>
                >>>>>>>one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
                >>>>>>>
                >>>>>>>Now I want to explode that array that was read so I use this:
                >>>>>>>
                >>>>>>>$arrayDa ta = explode("-", fgets($fd, 4096));
                >>>>>>>
                >>>>>>>print "first=$arrayDa ta[0]\n";
                >>>>>>>print "second=$arrayD ata[1]\n";
                >>>>>>>print "third=$arrayDa ta[2]\n";
                >>>>>>>print "fourth=$arrayD ata[3]\n";
                >>>>>>>
                >>>>>>>which will give me
                >>>>>>>
                >>>>>>>first=on e second=two third=three fourth=alpha beta kappa delta
                >>>>>>>epsilo n 13.5pp 29.95eu
                >>>>>>>
                >>>>>>>But I need to access the last two items (13.5pp and 29.95eu) of this
                >>>>>>>array to split them off as well so that the final output I need
                >>>>>>>would be
                >>>>>>>like this:
                >>>>>>>
                >>>>>>>first=on e second=two third=three fourth=alpha beta kappa delta
                >>>>>>>epsilon. txt numpages=13.5pp cost=29.95eu
                >>>>>>>
                >>>>>>
                >>>>>>
                >>>>>>>the length of the 'alpha beta kappa delta epsilon' will not always be
                >>>>>>>the same but the numpages and cost will always be the last two
                >>>>>>>elemen ts in the array. Any help would be appreciated.
                >>>>>>
                >>>>>
                >>>>>
                >>>>>I had thought I remembered at one time seeing a solution about
                >>>>>reversin g an array so that the last element was first, second to last
                >>>>>was second, etc. It just seems to be easier to explode the
                >>>>>$arrayDa ta[3], then reverse it (into say $revArrayData) and take the
                >>>>>reversed order as $revArrayData[1] and $revArrayData[2].
                >>>>>
                >>>>>Was I hallucinating about this or does something like it really exist?
                >>>>>
                >>>>
                >>>>Here: http://www.php.net/ bookmark it, use it. on the upper right
                >>>>there are two boxes: search for[ ] in the [ ][v]
                >>>>
                >>>>Enter: "reverse array" in the first box and select "functions list" or
                >>>>"whole site" in the second -- see what you find... Again. Bookmark it.
                >>>>Use it. you will be amazed at what you find...
                >>>>
                >>>
                >>>Thank you. I have gotten the reserse_array to work as I need. But I have
                >>>one more question that I can't find an answer to by searching.
                >>>
                >>>How can I print the $arrayData[3] and leave off the last two items
                >>>(13.5pp 29.95eu) so that the array would print as:
                >>>fourth=alp ha beta kappa delta epsilon
                >>>instead of
                >>>fourth=alp ha beta kappa delta epsilon.txt 13.5pp 29.95eu[/color]
                >>
                >>
                >>
                >> Use count($somearra y) to get back the number of elements.
                >> example:
                >> $arr[0]="hai";
                >> $arr[1]="hello";
                >> $arr[3]="hoe do ya do?";
                >>
                >> count($arr) will return 3.
                >>
                >> so... you should be able to figure it our yourself how to strip the last
                >> 2 elements after your explode.
                >>[/color]
                >
                > Thank you. I understand about finding out the count of the array and if
                > every line that was being read into the array was the same length, I
                > could figure out how to leave of the last two elements easily. But as I
                > stated earlier, the length of the 'alpha beta kappa delta epsilon' will
                > not always be the same but the numpages and cost will always be the last
                > two elements in the array.
                >
                > So I'm back to the question of how can I print the $arrayData[3] data
                > and leave off the last two elements when the length of the $arrayData[3]
                > data will vary from line to line?[/color]

                Hi Nathan,

                I think you misunderstand count.
                Or maybe I am misunderstandin g you. Could very well be at a fridayafternoon .
                :-)

                Count does not return the number of character, but the number of elements in
                an array.

                So if you do not know what is happening before 13.5pp 29.95eu, you can still
                take them off.

                alpha beta kappa delta epsilon 13.5pp 29.95eu
                or
                alpha beta 13.5pp 29.95eu

                could both be treated this way.


                So what you need to do is this:
                1) explode
                2) count the number of elements
                3) use only the first (total elements -2) to return.

                Here is an example.

                <?
                $test1 = "test gamma beta 13.5pp 29.95eu";
                $test2 = "alpha beta kappa delta epsilon 13.5pp 29.95eu";

                echo "stripLast2(".$ test1.")= ".stripLast2($t est1)."<br>";
                echo "stripLast2(".$ test2.")= ".stripLast2($t est2)."<br>";

                function stripLast2($som eString){
                $elements=explo de(" ",$someStri ng);
                $returnString = "";
                for ($i=0;$i<count( $elements)-2;$i++){$return String.=$elemen ts[$i]." "; }
                return $returnString;
                }
                ?>


                Regards,
                Erwin Moller

                Comment

                • Chung Leong

                  #9
                  Re: Need help with exploding array

                  "Nathan Rose" <nrose@spam.com > wrote in message
                  news:Mo3Vc.2711 $2L3.2058@newsr ead3.news.atl.e arthlink.net...[color=blue]
                  > Here's my problem. I am reading from a text file using this:
                  >
                  > if (!file_exists($ file))
                  > {
                  > echo "Don't exist\n";
                  > return false;
                  > }
                  >
                  > $fd = @fopen($file, 'r');
                  > if (!is_resource($ fd))
                  > {
                  > echo "Error reading\n";
                  > return false;
                  > }
                  >
                  > And it works fine. The data that will be read will always be in the text
                  > file in the form of
                  >
                  > one - two - three - alpha beta kappa delta epsilon 13.5pp 29.95eu
                  >
                  > Now I want to explode that array that was read so I use this:
                  >
                  > $arrayData = explode("-", fgets($fd, 4096));
                  >
                  > print "first=$arrayDa ta[0]\n";
                  > print "second=$arrayD ata[1]\n";
                  > print "third=$arrayDa ta[2]\n";
                  > print "fourth=$arrayD ata[3]\n";
                  >
                  > which will give me
                  >
                  > first=one second=two third=three fourth=alpha beta kappa delta epsilon
                  > 13.5pp 29.95eu
                  >
                  > But I need to access the last two items (13.5pp and 29.95eu) of this
                  > array to split them off as well so that the final output I need would be
                  > like this:
                  >
                  > first=one second=two third=three fourth=alpha beta kappa delta
                  > epsilon.txt numpages=13.5pp cost=29.95eu
                  >
                  > How can I do that? Do I need to further explode $arrayData[3] using a
                  > space delimiter? Is there a way to grab the last two fields only because
                  > the length of the 'alpha beta kappa delta epsilon' will not always be
                  > the same but the numpages and cost will always be the last two elements
                  > in the array. Any help would be appreciated.
                  >[/color]

                  This kind of stuff is easy as slicing cheese with regular expression.
                  Without regexp it's rather painful.

                  $s = "one - two - three - alpha beta kappa delta epsilon 13.5pp
                  29.95eu\r\n";
                  if(preg_match('/
                  \s*(.*?)\s*-
                  \s*(.*?)\s*-
                  \s*(.*?)\s*-
                  \s*(.*?)\s+
                  ([\d\.]+)\s*pp\s+
                  ([\d\.]+)\s*eu\s*
                  /xs', $s, $matches)) {
                  $one = $matches[1];
                  $two = $matches[2];
                  $three = $matches[3];
                  $four = $matches[4];
                  $numpages= $matches[5];
                  $cost= $matches[6];
                  print_r(compact ('one', 'two', 'three', 'four', 'numpages', 'cost'));
                  }


                  Comment

                  • Nathan Rose

                    #10
                    Re: Need help with exploding array

                    Erwin Moller wrote:

                    [color=blue]
                    > Count does not return the number of character, but the number of elements in
                    > an array.
                    >
                    > So if you do not know what is happening before 13.5pp 29.95eu, you can still
                    > take them off.
                    >
                    > alpha beta kappa delta epsilon 13.5pp 29.95eu
                    > or
                    > alpha beta 13.5pp 29.95eu
                    >
                    > could both be treated this way.
                    >
                    >
                    > So what you need to do is this:
                    > 1) explode
                    > 2) count the number of elements
                    > 3) use only the first (total elements -2) to return.
                    >
                    > Here is an example.
                    >
                    > <?
                    > $test1 = "test gamma beta 13.5pp 29.95eu";
                    > $test2 = "alpha beta kappa delta epsilon 13.5pp 29.95eu";
                    >
                    > echo "stripLast2(".$ test1.")= ".stripLast2($t est1)."<br>";
                    > echo "stripLast2(".$ test2.")= ".stripLast2($t est2)."<br>";
                    >
                    > function stripLast2($som eString){
                    > $elements=explo de(" ",$someStri ng);
                    > $returnString = "";
                    > for ($i=0;$i<count( $elements)-2;$i++){$return String.=$elemen ts[$i]." "; }
                    > return $returnString;
                    > }
                    > ?>[/color]

                    Sorry for not getting back to you sooner Erwin. I played around with
                    this example last night and got it working for my purposes. It's
                    throwing a few warnings on me but nothing I can't live with. This is an
                    internal script only. It will not be up on the web so I'll add a couple
                    of @ to suppress.

                    I didn't grasp the ($i=0;$i<count( $elements)-2;$i++) concept. That's why
                    I couldn't figure out how to drop only the last two.

                    Thank you for your assistance. I appreciate the effort you put forth to
                    help me.

                    Nathan

                    Comment

                    Working...