russthebarber Posted October 18, 2009 Share Posted October 18, 2009 hi, does anyone know how to use PHP to take the first few words of a text and limit them? i have seen this in many news sites where the news story headline reads like "woman found in obama's bedroom today at....... read more. ('read more' is a link) what i am looking for is something that says i will take the first 100 characters of the entry and then print "...read more" as a link to the full story. thanks Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/ Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 $entry = 'woman found in obama's bedroom today at blah blah blah blah'; $num = 35; //change to characters before cutting; if (strlen ($entry) > $num) { $entry = substr (ucwords (strtolower ($entry)), 0, $num).'...'; } play with that. Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-939402 Share on other sites More sharing options...
Alex Posted October 19, 2009 Share Posted October 19, 2009 I'd use a combination of wordwrap() and explode(). That way you can easily display only full words, so you don't get things like "Yesterday there was a h... read more". Ex: $str = "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."; $chars = 20; if(strlen($str) > $chars) echo end(array_reverse(explode("\n", wordwrap($str, $chars)))) . '... <a href="#">Read more</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-939409 Share on other sites More sharing options...
sastro Posted October 19, 2009 Share Posted October 19, 2009 I'm using substr then remove all chars after white space with preg_replace. Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-939421 Share on other sites More sharing options...
mrMarcus Posted October 19, 2009 Share Posted October 19, 2009 function limit_words ($input, $word_limit=25) { $words = explode (' ', $input); $discard = array_pop ($words); //lose the last word .. usually not a full word; $output = implode (' ', array_slice ($words, 0, $word_limit)); //reassemble input; return $output; } here's a function for words. you can append whatever you please to the end, ie. '...', 'more...', etc. Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-939424 Share on other sites More sharing options...
Philip Posted October 19, 2009 Share Posted October 19, 2009 I'd use a combination of wordwrap() and explode(). That way you can easily display only full words, so you don't get things like "Yesterday there was a h... read more". Ex: $str = "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."; $chars = 20; if(strlen($str) > $chars) echo end(array_reverse(explode("\n", wordwrap($str, $chars)))) . '... <a href="#">Read more</a>'; Can be shortened to: $str = "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."; $chars = 20; if(strlen($str) > $chars) echo current(explode("\n", wordwrap($str, $chars))) . '... <a href="#">Read more</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-939426 Share on other sites More sharing options...
russthebarber Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks to everyone who replied here. That's helped me a lot. Quote Link to comment https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/#findComment-940037 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.