tejama Posted March 20, 2007 Share Posted March 20, 2007 So far phpfreaks is 3 for 3 on solving my problems, so here we go again, hopefully its 4 for 4. I want to pull some info from a mysql DB and what I'm looking to do is to display only a snippet of text from a text field (ie: the first 30 words of the text entry). Can anyone suggest how I might go about doing this? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/ Share on other sites More sharing options...
hackerkts Posted March 20, 2007 Share Posted March 20, 2007 Try looking for substr(). Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211641 Share on other sites More sharing options...
suzzane2020 Posted March 20, 2007 Share Posted March 20, 2007 ya its gonna be substr($str,0,no of letters) Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211649 Share on other sites More sharing options...
DeathStar Posted March 20, 2007 Share Posted March 20, 2007 what about: mysql limit ?? Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211650 Share on other sites More sharing options...
suzzane2020 Posted March 20, 2007 Share Posted March 20, 2007 wud that be possible for a particular text? I guess mysql limit is for the number of records retrieved Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211652 Share on other sites More sharing options...
DeathStar Posted March 20, 2007 Share Posted March 20, 2007 Maybe this is vb but isnt there a limit command that only retrieves first few letters? Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211657 Share on other sites More sharing options...
suzzane2020 Posted March 20, 2007 Share Posted March 20, 2007 hmm..i dont think there is a limit function for the number of letters in a string..in php..havent come across one its generaly used for the number records in a database Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211658 Share on other sites More sharing options...
The Little Guy Posted March 20, 2007 Share Posted March 20, 2007 http://snippets.tzfiles.com/snippet.php?id=15 Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211675 Share on other sites More sharing options...
Barand Posted March 21, 2007 Share Posted March 21, 2007 try <?php $text = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nulla lacinia justo at velit. Nunc porttitor nisl porta mi. Curabitur sollicitudin purus vel felis. Maecenas nunc. Nulla facilisi. Integer vel ante sit amet ipsum consequat facilisis. Suspendisse hendrerit urna vel erat. Quisque auctor, erat nec ullamcorper sollicitudin, wisi leo hendrerit nisl, eu auctor turpis justo quis justo. Proin arcu metus, commodo quis, tincidunt varius, fermentum eget, lectus. Ut tellus. Sed nec dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nam sed magna."; $first30 = join (' ', array_slice (explode(' ', $text), 0, 30)); echo $first30; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211699 Share on other sites More sharing options...
Barand Posted March 21, 2007 Share Posted March 21, 2007 Although this probably more efficient <?php $k = $i = 0; $first30 = ''; while ($k < 30) { $c = $text{$i++}; $first30 .= $c; if ($c == ' ') $k++; } echo $first30; ?> Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211712 Share on other sites More sharing options...
grimmier Posted March 21, 2007 Share Posted March 21, 2007 couldn't you just explode the string with spaces as the seperator, and just output the first 30 elements of the array? Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211933 Share on other sites More sharing options...
tejama Posted March 21, 2007 Author Share Posted March 21, 2007 Thanks to all who helped. I looked at using 'limit', but didn't have much success. Substr however did the job and I achieved what I was hoping for. PHPFreaks is 4 for 4! Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-211936 Share on other sites More sharing options...
Barand Posted March 21, 2007 Share Posted March 21, 2007 couldn't you just explode the string with spaces as the seperator, and just output the first 30 elements of the array? As in my first example? Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-212275 Share on other sites More sharing options...
dlkinsey85 Posted March 21, 2007 Share Posted March 21, 2007 I see that this thread is solved but I'm a fan of the substr_replace() function. $example = "This line has more than 20 characters" echo substr_replace($example, "...", 20); would print: This line has more t... you can place the 3 periods with anything you want or leave it blank, " ", and use how ever many characters you want to display. Its a pretty useful function. Quote Link to comment https://forums.phpfreaks.com/topic/43575-solved-displaying-only-a-summary/#findComment-212302 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.