----- Original Message ----- From: "Rahul.Brenda" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Friday, March 07, 2003 11:22 AM Subject: [PHP] Displaying few words from record in mySQL
> Glory & Supreme Power > > What i'm looking to do is.. i want to display only the > first few words of the record in my mySQL database.. > > For example.. i have a table with a field "title".. > and let's say my last record has the value in title > field as > > "This is going to be really cool" > > What i want to display is > > "this is going..." > > Basically this is for News Headlines. I have a page > which displays news but on the first page of the site > i have to give the first few words of the last 4 > articles in the news table.. > > How can i do this? I have seen this in a lot of places > but i dont know how to do this. > > Thanks, > Rahul S. Johari I don't know how to do it within the SQL query.. or even if it's possible. But you can always gather the results as normal and build a function within php that chops up each line into words and uses the first few words as an abreviative text. If you want the strings to be of uniform length (or to ensure a minimum length) then you could do something with the strlen() and strpos() functions. This simple function that I wrote cares only about the number of words in the abreviation. It's rather crude but you're welcome to use it if it helps you.. function abreviate_text($str, $num_words) { // $str parameter must be an alpha numeric string if (!is_string($str)) return false; // $num_words parameter must be a non-zero integer value if (!is_integer($num_words) || $num_words == 0) return false; // split the string on all single spaces $words = explode(' ', $str); // build abreviated text.. $abr = array(); for($i=0; $i<$num_words; $i++) { $abr[] = $words[$i]; } $abr = implode(' ', $abr); $abr .= "..."; return $abr; } - Kevin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php