Only showing a few words of MySQL field

Discussion in 'PHP' started by mnymkr, Dec 3, 2006.

  1. #1
    I would like to show only say 15 words from a database field I call . it is a menu system that should show only part of the description and then upon clicking the whole things appears.

    Is this possible?
     
    mnymkr, Dec 3, 2006 IP
  2. kkibak

    kkibak Peon

    Messages:
    1,083
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Yes... do you want to do it using php or MYSQL? If you're okay w/using PHP, here's a way (this example assumes I've already pulled the info from my database and stored it in a variable called $yourString:

    
    
    echo implode(" ", array_slice(preg_split("/\s+/", $yourString), 0, 15));
    
    
    Code (markup):
    That would give you 15 words, a common addition is to add:
    
    ."... "
    
    Code (markup):
    so you get

    
    echo implode(" ", array_slice(preg_split("/\s+/", $yourString), 0, 15))."... ";
    
    Code (markup):
    That will give you 15 words and then a ...

    Hope that helps!
     
    kkibak, Dec 3, 2006 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    hey thats great.

    i will go look up the php so i know what i am doing.

    does that show words or characters?
     
    mnymkr, Dec 3, 2006 IP
  4. kkibak

    kkibak Peon

    Messages:
    1,083
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thats's words, and it determines what a word is by searching for the " " as you see in the first part of implode.

    glad to help
     
    kkibak, Dec 3, 2006 IP
  5. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #5
    i've never quite understood what it meant when "" was used

    but that is why we learn everyday
     
    mnymkr, Dec 3, 2006 IP
  6. kkibak

    kkibak Peon

    Messages:
    1,083
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ah just to be sure i was clear, when i mentioned " " in my last post above i meant that we are basically searching the string and separating by spaces--the " " represents the blank space between each word.
     
    kkibak, Dec 3, 2006 IP
  7. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    If you don't mind using character limitations rather then word limitations a simple way to accomplish this is

    
    substr($variable, 0 ,50);
    
    PHP:
    Variable being the target string and 50 being the amount of characters you wish to have. (0 is just where you want to begin in the string)

    preg_split can be alittle intensive on Apache so if you are performing these alot, it may be worth using a different aproach if server resources are important.
     
    Luke, Dec 4, 2006 IP
  8. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Can this be done by the SQL query instead of the PHP. just curious.

    never quite understood the implode thing btw. read about it but it isn't clicking.
     
    mnymkr, Dec 5, 2006 IP
  9. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yes, you can do this in MySQL by characters. Read up on SUBSTRING

    SELECT SUBSTRING(description,0,50) FROM table WHERE id = 1;
     
    T0PS3O, Dec 5, 2006 IP
  10. kkibak

    kkibak Peon

    Messages:
    1,083
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks for posting the MySQL option... just out of curiosity, do you know of a way to limit to a number of words as opposed to characters w/out using PHP?

    Implode basically takes an array and makes it one string, separating each element in the array by whatever you tell it to (in the example above a space as you see by " ").
     
    kkibak, Dec 5, 2006 IP