Jump to content

[SOLVED] Easiest question you'll ever answer (please :)


br3nn4n

Recommended Posts

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.

Link to comment
Share on other sites

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) ? '&#8230;' : $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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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) . '...';
?>

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

Link to comment
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.