Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/178161-solved-first-few-characters-only/
Share on other sites

$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.

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>';

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.

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>';

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.