Re: Displaying few words from record in mySQL

From: Date: Fri, 07 Mar 2003 20:21:43 +0000
Subject: Re: Displaying few words from record in mySQL
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-138690@lists.php.net to get a copy of this message
Kevin, Your program certainly makes very good sense to me. However, 2 things i will need to ask you.. is that, let's say i decide i want the first 4 words from the record displayed.. for all the 4 rows i'm displaying.. where do i define that in your program? And secondly, how do i make this program work on the myrow[myfield] variable which will actually carry the record from the database? thanks, Rahul > 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 > __________________________________________________ Do you Yahoo!? Yahoo! Tax Center - forms, calculators, tips, more http://taxes.yahoo.com/

« previous php.general (#138690) next »