Hello People
I know how to explode individual lines from a text file:
<?php
$array = file("tempfile.txt");
foreach($array as $row) {
$line = explode('|', $row);
print " $line[1], $line[2],etc";
}
?>

But every time I try to explode all of the information in the file I just get the word 'Array' printed, or in the case of the last attempt it printed 'Array' 2000+ times. Could someone please help me to just display the text minus the '|'deliniator of course?

Many thanks for any help offered...

John Walker

    Open the file and explode it by line termination (\n)
    That breaks it into an array of lines.
    Explode the line you need by your delimiter. (|)

      $array = file("tempfile.txt");

      this produces an array
      the function reads line per line
      it means every ended line will be saved to $array
      beggining from line one will be saved into $array[0], $array[1] etc
      when you echo $array, you will get 'Array'
      but when you echo $array[0], you will get the first line in tempfile.txt

        Hello bernouli
        Sorry for any confusion, I know how the example I gave works, what I was asking was how do I get all the information from the textfile without having to specify every '$line[#]' manually in the code, as the textfile will vary in length so I want to dynamically grab ALL the entries in the textfile, is there a way to do this?
        Cheers John

          $open_file=fopen("tempfile.txt","r");
          while(! feof ($open_file)) $string=fread($open_file,1000000);

            Hello
            Could you possible expand on the code snippet you wrote?
            I tried finishing it off myself in numerous 'educated guesses' but all I get is 'Resource id #1' . Again thanks for your responses so far, I use this forum when I cannot resolve something myself (not through want of trying), and any help I get goes to furthering my understanding of PHP, just saying that as I know some posts on the forum are just asking people to 'do the work for them', and just pointing out this isn't one of those cases. Honest Guvnor

            Cheer John🙂

              ok what i have wrote is

              $open_file=fopen("tempfile.txt","r");
              //open the file name tempfile.txt, tiwh option "r", it means ist in read only mode

              while(! feof ($open_file))
              //do this before the file reachs its end (feof)=(end of file)

              $string=fread($open_file,1000000);
              //read the file up to 1 MB and save it to $string

                Thanks again for expanding on that it works great:

                <?php
                $open_file=fopen("tempfile.txt","r");
                while(! feof ($open_file)) $string=fread($open_file,1000000);
                echo $string;
                ?>

                , however is there a way of exploding the $string to get rid of the deliniator '|'
                I tried :

                <?php
                "your code"
                $info = explode('|', $string);
                echo"$info";
                ?>

                Which gives me my old adversay Array. I need the deliniator in so I can echo the information back into a form at a later date, which I've cracked I just need display the tempfile without the '|' in it. How would I incorporate 'explode' into the code you (kindly) provided to achieve this? I'm trying not to take the 'p*ss' seeking by seeking loads of help on this, everyones time is appreciated.

                Cheers John

                  when you do that
                  $info = explode('|', $string);

                  you get an $array again, because explode() just turn it into array

                    hello
                    Will the $string allow me to replace the deliniator, as I'm guessing now that explode isn't the function for the task 😉 so would something this eliminate the pesky '|':

                    $open_file=fopen("tempfile.txt","r");
                    while(! feof ($open_file))
                    $string = fread($open_file,1000000);
                    $myresult = eregi_replace('|','',$string);
                    echo $myresult ;

                    Apart from it gives me the error 'REG_EMPTY', at least it isn't 'Array'. Is this a good alternative function to persue?

                    Cheers John

                      Hello bernouli

                      I cracked it with this :

                      <?php
                      $open_file=fopen("tempfile.txt","r");
                      while(! feof ($open_file))
                      $string = fread($open_file,1000000);
                      $myresult = str_replace('|','',$string);
                      echo $myresult ;
                      ?>

                      I just used this to get rid of the '|' :
                      $myresult = str_replace('|','',$string);
                      Thanks for all your time helping me work it through, your input was greatly appreciated!

                      Cheers John

                        hello john,
                        thats why this forum exists, to help each other ,)

                          Implode rocks as well. Try this:

                          <?php
                          $body = file("somefilenamehere");
                          
                          # Here we add <BR>s
                          print implode("<BR>\n",$body);
                          #Now we put it in a table:
                          print "<TABLE><TR><TD>";
                          print implode("</TD></TR>\n<TR><TD>",$body);
                          print "</TD></TR></TABLE>";
                          
                          # Another novel example of using implode:
                          
                          $array = ( "name" => "john", 
                                             "address" => "California", 
                                             "phone" => "210-555-8989");
                          
                          #What are the keys to our array?
                          print implode(":",array_keys($array));
                          # What values are stored in it?
                          print implode(":",array_values($array));
                          ?>
                          
                            Write a Reply...