Hello, I'd really need some help.
My english may be a little bad so sorry for this :rolleyes:

I'm new to php and trying to learn correctly, but I havn't been able to find how to do it.
Here is what I'm trying to do:

in a text file there are links separated like this

<div class="links>link 1</div>#enddiv
<div class="links>link 2</div>#enddiv
<div class="links>link 3</div>#enddiv
...
<div class="links>link 150</div>#enddiv

now, lets say I have 3 html pages to separate those links.

On page 1, I want to have links 1 to 50
On page 2, links 51 to 100
On page 3, links 101 to the rest (I'm keeping last page for all the rest of links so maybe there are less or more)

for now on, I'm stuck with the following code and don't know what to continue with (probly strtok but doesn't know how to do it.)

<?php
 $links = "links.txt";
 $fd = fopen ($links, "r");
 $content = fread ($fd,filesize ($,links));
 fclose ($fd); 
 $delimiter = "#enddiv";
?>

Thanks in advance for the help, I'm really confused, php is a new world for me 😃

    1. First, have a look at [man]file_get_contents[/man]; save you a bit of typing.

    2. Have a look at the result of [man]explode[/man]($delimiter, $content).

    3. Both of those functions have duals you might find useful as well: [man]join[/man] and [man]file_put_contents[/man].

      Weedpacket;10994804 wrote:
      1. First, have a look at [man]file_get_contents[/man]; save you a bit of typing.

      2. Have a look at the result of [man]explode[/man]($delimiter, $content).

      3. Both of those functions have duals you might find useful as well: [man]join[/man] and [man]file_put_contents[/man].

      I'm trying everything, reading every forums, and not finding anything to do what I'd like.. I'm able to read the file, separate my divs one by one, but this is not what I'm searching for.. :bemused:

        perhaps you could offer some code that shows what you are trying and explain what's wrong with it? that would be a good starting point.

          sneakyimp;10994824 wrote:

          perhaps you could offer some code that shows what you are trying and explain what's wrong with it? that would be a good starting point.

          Ok here's what I did until now.

          <?php
           $links = "links.txt";
           $fd = fopen ($links, "r");
           $content = fread ($fd,filesize ($links));
           fclose ($fd);
           $delimiter = "#enddiv";
           $splitcontent = explode($delimiter, $content);
           ?>
          
           <?
           foreach ( $splitcontent as $divs )
          {
           echo "$divs";
          }
          ?>
          ?>

          Every html pages will have this code to separate links.txt.
          With this code, the page is showing all the content. I'd like to know which code I could use to limit the number of results shown to "50".
          For the second page, I'd want to start from 51st $delimiter and go to 100th $delimiter.
          On the third one, I want to start from 101st $delimiter, and go to the rest of the document.
          Here is my problem, I don't know what to use to do this.

            Well if you have all the divs in an array, $divs, and you know what page you are on, you just need to loop through $divs[$start] to $divs[$end]. To show the divs that belong on that page. The whole trick is to somehow specify which page you want (probably by specifying it in the query string like your_page.php?page=1) and then retrieve that within your scrip ($current_page = $_GET['page']) and then determining what $start and $end are.

              sneakyimp;10994883 wrote:

              Well if you have all the divs in an array, $divs, and you know what page you are on, you just need to loop through $divs[$start] to $divs[$end]. To show the divs that belong on that page. The whole trick is to somehow specify which page you want (probably by specifying it in the query string like your_page.php?page=1) and then retrieve that within your scrip ($current_page = $_GET['page']) and then determining what $start and $end are.

              For now I'm going to add the code to each page, and separates it manually like this
              Page 1 = $start = 0; $end = 50
              Page 2 = $start = 51; $end = 100
              Page 3 = $start = 101; $end = end of txt file

              Is there a way I could fix it like this?

                KingdomTubes;10994885 wrote:

                For now I'm going to add the code to each page, and separates it manually like this
                Page 1 = $start = 0; $end = 50
                Page 2 = $start = 51; $end = 100
                Page 3 = $start = 101; $end = end of txt file

                Is there a way I could fix it like this?

                I finally found what I was searching for!! Fixed my problem with array_slice

                 <?php
                 $links = "links.txt";
                 $fd = fopen ($links, "r");
                 $content = fread ($fd,filesize ($links));
                 fclose ($fd);
                 $delimiter = "#enddiv";
                 $splitcontent = explode($delimiter, $content);
                
                for page 1 : $output = array_slice($splitcontent, 0, 50);
                
                for page 2 : $output = array_slice($splitcontent, 51, 100);
                
                for page 3 : $output = array_slice($splitcontent, 101);
                
                <?php
                 foreach ( $output as $divs )
                {
                 echo "$divs";
                }
                ?>
                

                Thanks a lot, this helped me to find these 🙂

                  Write a Reply...