Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/125609-solved-limit-output/
Share on other sites

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");

Link to comment
https://forums.phpfreaks.com/topic/125609-solved-limit-output/#findComment-649421
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.