thomashw Posted April 29, 2008 Share Posted April 29, 2008 I have different paragraphs in a database, and I want a way to be able just to show "previews" of the paragraphs - maybe something like the first 100 words. Is there a way to limit how many characters are called from the database, or how many are shown? Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/ Share on other sites More sharing options...
DarkWater Posted April 29, 2008 Share Posted April 29, 2008 SELECT paragraph FROM table WHERE whatever='whatever'; Select your stuff, then do this: $shortened = substr(0, 100, $row['paragraph']) $shortened .= '...'; echo $shortened; Obviously, this is in the while loop from the mysql_fetch_array calls. Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/#findComment-529281 Share on other sites More sharing options...
Fadion Posted April 29, 2008 Share Posted April 29, 2008 Try this: <?php $str = 'Long teeeeeeext'; $str = substr($str, 0, 100); $dotPos = strrpos($str, '.'); //find last occurrence of "." $str = substr($str, 0, $dotPos); echo $str; ?> The code will trim the text to 100 character, find the last occurrence of a dot and trim the text again, so u have full senteces. U can use the strrpos() for a space (" ") if u dont want to show full sentences. Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/#findComment-529283 Share on other sites More sharing options...
Fadion Posted April 29, 2008 Share Posted April 29, 2008 $shortened = substr(0, 100, $row['paragraph']) Isnt that syntax of substr() incorrect? Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/#findComment-529284 Share on other sites More sharing options...
thomashw Posted April 29, 2008 Author Share Posted April 29, 2008 Thanks for the help, I should be able to figure it out from here. Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/#findComment-529287 Share on other sites More sharing options...
DarkWater Posted April 29, 2008 Share Posted April 29, 2008 $shortened = substr(0, 100, $row['paragraph']) Isnt that syntax of substr() incorrect? Yup, wasn't paying attention. xD $shortened = substr($row['paragraph'], 0, 100)' >_> Link to comment https://forums.phpfreaks.com/topic/103347-solved-limited-characters/#findComment-529289 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.