Limited xxx Words

Discussion in 'PHP' started by FishSword, Oct 28, 2009.

  1. #1
    Hiya!

    I Have a field in a database, that contains the following text (HTML and text)

    How do I display only the first 12 words of the field, along with "..." at the end, whilst keeping the formatting in tact? e.g. <br> for new paragraph?

    Example:

    The quick brown fox jumps over the lazy dog

    The quick brown...
     
    FishSword, Oct 28, 2009 IP
  2. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you can split the text into an array using the spaces and then loop through the array up to the number of words you want to display
     
    adwebtiser, Oct 28, 2009 IP
  3. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    How do I do that?
    I also need to add "..." if the word limit has been set to less than the total number of words in the database.
     
    FishSword, Oct 28, 2009 IP
  4. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    for spliting you just do this:

    $text="<p>The quick brown fox jumps over the lazy dog</p><br />
    <p>The quick brown fox jumps over the lazy dog</p>";

    $textarray = split(" ",$text);

    then you loop the array:

    $num=12 // your choice of how many words
    for($i=0;$i<$num;$i++) {
    $newtxt .= $textarray[$i];
    }

    $newtxt .= "..."

    Thats all
     
    adwebtiser, Oct 28, 2009 IP
  5. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    what error do you get?
     
    adwebtiser, Oct 28, 2009 IP
  6. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #6
    Nevermind. How do you add the spaces back between the words?
     
    FishSword, Oct 28, 2009 IP
  7. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ahh i missed to put that in the code lol

    here is the corrected version:

    $num=12 // your choice of how many words
    for($i=0;$i<$num;$i++) {
    $newtxt .= $textarray[$i]." ";
    }
     
    adwebtiser, Oct 28, 2009 IP
  8. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #8
    If you limit the words by 9, is it possible to add the "..." to the end of the first "The quick brown fox jumps over the lazy dog", as at the moment, there is no way of telling that the field has been cut short, as there is no "..." whilst the limit is set to 9 ;)
     
    FishSword, Oct 28, 2009 IP
  9. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    It should be
     
    adwebtiser, Oct 28, 2009 IP
  10. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #10
    ah, that's what the problem is. There's a space between <br and />
     
    FishSword, Oct 28, 2009 IP
  11. FishSword

    FishSword Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #11
    Why does the last 3 words from the second "the quick brown fox jumps over the lazy dog" get cut off, also is there anyway to stop it?

    <?php
    $text="<p><strong>the quick brown fox jumps over the lazy dog</strong> <br /> <br /> <strong>the quick brown fox jumps over the lazy dog</strong></p>";
    
    $textarray = split(" ",$text);
    
    $num=18; // your choice of how many words
    for($i=0;$i<$num;$i++) {
    $newtxt .= " ".$textarray[$i];
    } 
    
    echo $newtxt .= "...";
    ?>
    PHP:
     
    Last edited: Nov 1, 2009
    FishSword, Nov 1, 2009 IP
  12. adwebtiser

    adwebtiser Peon

    Messages:
    95
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Its because of the spaces in your "<br />" tags and between them so change it to "</strong><br/><br/>"
     
    adwebtiser, Nov 3, 2009 IP
  13. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #13
    DO NOT USE SPLIT! It has been deprecated and removed from PHP, instead use Explode. also, here is a great way to do what you want:

    
    <?php
    $text="<p><strong>the quick brown fox jumps over the lazy dog</strong> <br /> <br /> <strong>the quick brown fox jumps over the lazy dog</strong></p>";
    
    $num=18; // your choice of how many words
    $Cut = implode(" ",array_splice(explode(" ",$text),0,$num)));
    
    PHP:
    There you go!
     
    NatalicWolf, Nov 3, 2009 IP