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?
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!
hey thats great. i will go look up the php so i know what i am doing. does that show words or characters?
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
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.
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.
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.
Yes, you can do this in MySQL by characters. Read up on SUBSTRING SELECT SUBSTRING(description,0,50) FROM table WHERE id = 1;
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 " ").