Vivid Lust Posted February 3, 2009 Share Posted February 3, 2009 Hey all, Could someone tell me how in PHP, I could turn a whole paragraph into X amount of words?? Thanks for any help. Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/ Share on other sites More sharing options...
premiso Posted February 3, 2009 Share Posted February 3, 2009 substr is one way...explode would have to be another. Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-753852 Share on other sites More sharing options...
Maq Posted February 3, 2009 Share Posted February 3, 2009 Could you be a little more descriptive? How would you like to do this? Cut off the end of the paragraph after a certain amount of words? You could just explode the paragraph and only keep the first x amount of words... but again, need more detail. Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-753853 Share on other sites More sharing options...
Vivid Lust Posted February 3, 2009 Author Share Posted February 3, 2009 What im doing, is getting a forum post, I want to limit it to X words, so its a kind of excerpt. Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-753857 Share on other sites More sharing options...
Philip Posted February 3, 2009 Share Posted February 3, 2009 Get the length with strlen, if its longer than you want, chop it off with substr Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-753869 Share on other sites More sharing options...
Vivid Lust Posted February 4, 2009 Author Share Posted February 4, 2009 Thanks for the help, however im finding it hard to use substr. Could some please post me the code, so that I can turn: "With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post." Into: "With Quick-Reply you can write" There the code limits the paragraph to the first 5 words. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-754418 Share on other sites More sharing options...
Maq Posted February 4, 2009 Share Posted February 4, 2009 * Not tested * - This SHOULD echo the first 4 words of the string. $string = "With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post." $short = ""; $pieces = explode(" ", $string); for($i = 0; $i $short .= $pieces[$i] . " "; echo $short; Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-754425 Share on other sites More sharing options...
premiso Posted February 4, 2009 Share Posted February 4, 2009 This is assuming you want it to be cut into 10 words, roughly 50 characters: <?php $text = "With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post."; if (strlen($text) > 50) { $chopped = substr($text, 0, 50); echo $chopped . "..."; }else { echo $text; } ?> Simple as that. Should do it. EDIT: This is an example using substr Quote Link to comment https://forums.phpfreaks.com/topic/143672-solved-php-condense-text/#findComment-754432 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.