br3nn4n Posted December 2, 2008 Share Posted December 2, 2008 I have some text. I want to only display a certain amount of it. I want to cut it off after maybe 25 words (I'm thinking after so many characters would be much easier though). I also want to add 3 periods after the cut off point. Example: Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sed tellus. Integer tincidunt erat a odio. Curabitur metus. Cum sociis natoque penatibus et magnis dis parturient montes, nascet... Simple, simple stuff. I'm thinking wrap($article, 80) and then $article=$article . "..."; Thanks so much. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 substr Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted December 2, 2008 Share Posted December 2, 2008 That limits the amount of chars not words, you would need to mix word count and conditional statements. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted December 2, 2008 Author Share Posted December 2, 2008 I don't care if it slices a word in half, not really. Unless you wanna go into further detail about that mixing of word count and while. actually, I only really need to know this- is there a built in function to count words? Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 2, 2008 Share Posted December 2, 2008 Nabbed from Kohana (CodeIgnitor): <?php /** * Limits a phrase to a given number of words. * * @param string phrase to limit words of * @param integer number of words to limit to * @param string end character or entity * @return string */ function limit_words($str, $limit = 100, $end_char = NULL) { $limit = (int) $limit; $end_char = ($end_char === NULL) ? '…' : $end_char; if (trim($str) === '') return $str; if ($limit <= 0) return $end_char; preg_match('/^\s*+(?:\S++\s*+){1,'.$limit.'}/u', $str, $matches); // Only attach the end character if the matched string is shorter // than the starting string. return rtrim($matches[0]).(strlen($matches[0]) === strlen($str) ? '' : $end_char); } ?> The default $end_char is an ellipses. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted December 2, 2008 Author Share Posted December 2, 2008 Aw sweet, thanks. I also just found this - http://us2.php.net/str_word_count Quote Link to comment Share on other sites More sharing options...
Garethp Posted December 2, 2008 Share Posted December 2, 2008 Um... $String = "Your string here"; if(strlen($String) > 25) { $String = substr($String, 0, 25); $String .= "..."; } EDIT: Sorry, I missed the word part, bit tired... Quote Link to comment Share on other sites More sharing options...
haku Posted December 2, 2008 Share Posted December 2, 2008 substr is only for characters, not for words. --------------------------------------- I don't know if there is a more efficient way than this, but probably there is: $text = "some text"; // make this lots of words. $words = explode(" ", $text); $words25 = array_slice($words, 0, 24); // note: make the second number one less than the number of words you want $string = implode(" ", $words25); $string .= '...'; note: untested. edit: tested and works. Quote Link to comment Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 oops my bad. Thought OP said chars not word. Slight variation of haku's... <?php $string = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; $result = explode(' ',$string,26); array_pop($result); $result = implode(' ',$result) . '...'; ?> Quote Link to comment Share on other sites More sharing options...
haku Posted December 2, 2008 Share Posted December 2, 2008 Learn something new every day. I didn't realize that you could limit the number of elements in an explode. But why didn't you just set it at 25 in the first place, removing the necessity for the array_pop? Quote Link to comment Share on other sites More sharing options...
.josh Posted December 2, 2008 Share Posted December 2, 2008 when you put a limit on the explode, if there's more matches than the limit, the rest is put into a string in the last element. Quote Link to comment Share on other sites More sharing options...
br3nn4n Posted December 2, 2008 Author Share Posted December 2, 2008 I love you people. I'll be trying these out, looks like they all should do pretty much what I want. Thank you so much! EDIT: tried Crayon's and it works perfectly. Quote Link to comment Share on other sites More sharing options...
haku Posted December 2, 2008 Share Posted December 2, 2008 Good to know Crayon. Thanks. Quote Link to comment Share on other sites More sharing options...
haku Posted December 3, 2008 Share Posted December 3, 2008 I've thought about this, and I think the code needs to be re-written a little: $string = "this is a sentence."; $result = explode(' ',$string,26); if(count($result) == 26) { array_pop($result); } $count = count($result); $result = implode(' ',$result); if($count == 25) { $result .= '...'; } echo $result; Quote Link to comment Share on other sites More sharing options...
.josh Posted December 3, 2008 Share Posted December 3, 2008 I see what you're getting at, but one would hope that the text is going to be more than 25 words long in the first place, from a content population PoV (that is, one would hope that one is not submitting 25 word articles (or whatever) in the first place...). But nonetheless, if you do want to script for that, you're not accounting for the possibility that it could be say...20 words. Quote Link to comment Share on other sites More sharing options...
haku Posted December 3, 2008 Share Posted December 3, 2008 I agree with you that it's not likely that someone is going to post an article shorter than 25 words! But you are right about not accounting for less than 25 words (as far as adding the '...' goes that is. The array pop accounts for less than 25 words). So I guess it should be: $string = "this is a sentence."; $result = explode(' ',$string,26); if(count($result) == 26) { array_pop($result); } $count = count($result); $result = implode(' ',$result); if($count <= 25) { $result .= '...'; } echo $result; Quote Link to comment Share on other sites More sharing options...
flyhoney Posted December 3, 2008 Share Posted December 3, 2008 I'm telling you, use the function I posted: http://www.phpfreaks.com/forums/index.php/topic,228052.msg1053252.html#msg1053252 It's a part of a framework and it's well tested. Quote Link to comment 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.