Split pages into two halves

Discussion in 'PHP' started by Astroman, Aug 11, 2009.

  1. #1
    Say I have a page of text attached to $page, so
    $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind';
    Code (markup):
    Does anyone know how I can split it up into an $intro and a $main, so the $intro bit displays the first 10 words and the $main display everything but the first 10 words?

    I tried doing a truncate type thing to get the $intro, then eregi_replace to remove the content of $intro from $page to make $main, but it didn't really work properly.

    So I thought there might be a better way, any ideas?
     
    Astroman, Aug 11, 2009 IP
  2. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #2
    Is this dynamic? If not, why don't you have 2 variables?
    $intro = 'the intro text';
    $main = $intro.' even more text!';
    PHP:
    or do something like:
    $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind';
    $splits = explode(" ", $page);
    array_splice($splits, 10);
    $intro = implode(" ", $splits);
    PHP:
     
    Sky AK47, Aug 11, 2009 IP
  3. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #3
    Yes it's dynamic.
     
    Astroman, Aug 11, 2009 IP
  4. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #4
    OK, so the second did the work?
     
    Sky AK47, Aug 11, 2009 IP
  5. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #5
    How would I create the $main part using your example?
     
    Astroman, Aug 11, 2009 IP
  6. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #6
    Don't know how to get that wit array_splice, but use preg_replace instead;
    $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind';
    $splits = explode(" ", $page);
    array_splice($splits, 10);
    $intro = implode(" ", $splits);
    $main = preg_replace("/$intro/i", "", $page);
    PHP:
    Tested it :)
     
    Sky AK47, Aug 11, 2009 IP
    Astroman likes this.
  7. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Thanks, I will give it a try tomorrow. What is the /i bit for?
     
    Astroman, Aug 11, 2009 IP
  8. Sky AK47

    Sky AK47 Member

    Messages:
    298
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    45
    #8
    The slashes in the preg_replace functions as delimiter and the i to ignore case sensitivity. This whole process is called 'RegEx'. (Regular Expressions)
     
    Sky AK47, Aug 11, 2009 IP
  9. astrazone

    astrazone Member

    Messages:
    358
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    33
    #9
    if you want to split text into multiple pages(dynamicly) I have the script for it.
    by the way this is what you need:
    <?php
    $page = 'all my text in here for the whole page is written down by me in the most audacious fashion known to ferret-kind';
    $splits = explode(" ", $page);
    $count = count($splits);
    $half = round($count / 2) ;
    $arrayslice = array_splice($splits,0,$half,$else);
    $firsthalf = implode(" ",$arrayslice);
    $secondhalf = implode(" ",$splits);
    echo $firsthalf;//firsthalf
    echo "<br/>";
    echo $secondhalf;//secondhalf
    ?>
    PHP:
     
    Last edited: Aug 11, 2009
    astrazone, Aug 11, 2009 IP
    Astroman likes this.
  10. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #10
    Thanks astrazone, I will try that too. I was just looking over my previous posts and realized I didn't reply to you.
     
    Astroman, Sep 3, 2009 IP
  11. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    substr(); is your answer!
    
    $string = "ALL YOUR TEXT";
    $start = 0;
    $end = 10;
    echo substr($string, $start, $end);
    
    echo substr($string, $end, 6000);
    
    Code (markup):
     
    yuvrajm, Sep 4, 2009 IP
    Astroman likes this.
  12. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #12
    yuvrajm, that works too - I gave you a plus rep also.

    Now if I could figure out how to make it so I can put code 10 in from the end instead of 10 characters from the beginning that would be uber cool.
     
    Astroman, Sep 5, 2009 IP
  13. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #13
    Oh wait, just realized I already know the answer, just make the line $end = 10; into $end = -10;
     
    Astroman, Sep 5, 2009 IP
  14. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #14
    This works really well, but how can I alter it it to count words instead of characters, or even just count white spaces so it doesn't chop words in half?

    I know all the examples do that but this substr method seems the easiest so far.
     
    Astroman, Sep 7, 2009 IP
  15. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    got this solution from a website.
    This $no will count words, but not whitespaces... for that you may simple make 2x of $no so whitespace may also be counted. I have not tested thus one till now.
    
    function count_words($str)
    {
    $no = count(explode(" ",$str));
    return $no;
    }
    
    Code (markup):
     
    yuvrajm, Sep 7, 2009 IP
  16. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #16
    Great, but how do you achieve the same sort of thing as your first example using that?
     
    Astroman, Sep 7, 2009 IP
  17. hamidof

    hamidof Peon

    Messages:
    619
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Nope you need something like:

    
    preg_match('#((?:\S+\s+){10})(.*)#s', $page, $matches);
    $header = $matches[1];
    $main = $matches[2];
    
    PHP:
    Good Luck!
     
    hamidof, Sep 7, 2009 IP
    Astroman likes this.
  18. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Agreed! Actually I'm a little starter in regular expressions.

    You are right, this is the best way to count chars in a string.
     
    yuvrajm, Sep 8, 2009 IP
  19. Astroman

    Astroman Well-Known Member

    Messages:
    2,355
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    135
    #19
    Well I can't even understand what that one's doing so I don't know how to get it to do the insert logo/ad thing after so many words?
     
    Astroman, Sep 9, 2009 IP
  20. yuvrajm

    yuvrajm Peon

    Messages:
    52
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #20
    What exactly do you want, just make a half of the page or make a complex query with multiple substr?
     
    yuvrajm, Sep 9, 2009 IP