Bricktop Posted September 24, 2008 Share Posted September 24, 2008 Hi all, I wonder if someone could tell me how I limit MySQL output? Essentially, I have a script that outputs some "Latest News" text - on my main "News" page I would like this output to be limited to say the first 50 words and then a "Read More" link will be clickable to take the reader to the full story. Just wondered on the syntax required to limit the output to the first x amount of words. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/125609-solved-limit-output/ Share on other sites More sharing options...
JasonLewis Posted September 24, 2008 Share Posted September 24, 2008 function limit_words($str, $limit, $extension=""){ $words = explode(" ", $str, $limit + 1); //We plus 1 because limiting the explode causes the remaining words to be placed in the last key. array_pop($words); return implode(" ", $words).$extension; } echo limit_words("Hey this will be limited to 5 words!", 5, "... Read More"); Quote Link to comment https://forums.phpfreaks.com/topic/125609-solved-limit-output/#findComment-649421 Share on other sites More sharing options...
Bricktop Posted September 24, 2008 Author Share Posted September 24, 2008 Thanks matey - I'll give it a go! Quote Link to comment https://forums.phpfreaks.com/topic/125609-solved-limit-output/#findComment-649438 Share on other sites More sharing options...
CroNiX Posted September 25, 2008 Share Posted September 25, 2008 If you want a more consistent look of all of the entries, I would limit by characters, not words. Quote Link to comment https://forums.phpfreaks.com/topic/125609-solved-limit-output/#findComment-649984 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.