Remove long text

Discussion in 'PHP' started by badmasketa, Jun 5, 2008.

  1. #1
    Can anybody post me the function to remove off the long text to lil bit short like into 100 words only??

    suppose there is a long text more than 200 words then that function should only show 100 words and at the last should show ... and link for more


    thanks
     
    badmasketa, Jun 5, 2008 IP
  2. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is an example:
    <?
    //test it with some short text
    $text = "some short text that doesn't need to be cut";
    cut_text($text);
    
    
    printf("<hr />");
    
    //now something long enough to be cut
    $text = '';
    for($i = 0; $i < 255; $i++)
      $text .= "long ";
    
    cut_text($text);
    
    
    function cut_text($text, $words = 200)
    {
      $c = 0;
      $len = 0;
    
      for($i = 0; $i < strlen($text); $i++)
      {
        if($text{$i} == ' ')
          $c++;
    
        if($c == $words)
        {
          $len = $i;
          break;
        }
      }
    
      if($len)
        printf("<div id='cut_text'>%s<br />
        <div style='display: none' id='other_text'>%s</div>
        <input type='button' value='Show More' onclick=\"document.getElementById('other_text').style.display='inline'\">
        </div>",
        substr($text, 0, $len),
        substr($text, $len, strlen($text)-$len));
      else
        printf("<div id='cut_text'>%s</div>", $text);
    }
    ?>
    
    PHP:
    You can change the number of words after which the script will cut the text, just pass second argument to cut_text().

    You owe me at least a beer for that one ;)
     
    xlcho, Jun 6, 2008 IP
  3. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    thanks for the script xlcho ;)
     
    mehmetm, Jun 6, 2008 IP