Display only the first 3-4 words from a huge text

Discussion in 'PHP' started by Kyriakos, Sep 6, 2010.

  1. #1
    Hi,

    It's possible to display the first 3 or 4 words from a huge text field of a mysql database?

    thanks in advance.
     
    Kyriakos, Sep 6, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Yes, it is possible.
     
    s_ruben, Sep 6, 2010 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I want a PHP script if it possible. Not only the answer "yes or no".
     
    Kyriakos, Sep 6, 2010 IP
  4. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #4
    If you want PHP script write that you want PHP script! :) I just answer your question! :)

    You can do by the PHP functions explode(). Get the huge text from the mysql database, explode the text by space ( $exploded_huge_text = explode(" ",$huge_text) ), then concatenate the first 3 or 4 words ( $string = $exploded_huge_text[0]." ".$exploded_huge_text[1]." ".$exploded_huge_text[2]." ".$exploded_huge_text[3] ). I think this is what you want.
     
    s_ruben, Sep 6, 2010 IP
  5. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    depending on txt size maybe quicker doing
    
    $start = 0;
    $cnt = 0;
    $outstr = "";
    while ($cnt<3) {
     $sub = substr($huge,$start,$start+1);
     $start++;
     if ($sub=="") $cnt++;
     $outstr .= $sub;
    }
    
    PHP:
     
    themullet, Sep 6, 2010 IP
  6. Sulan84

    Sulan84 Member

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #6
    Sulan84, Sep 6, 2010 IP