1
str_limit($one->body, $limit = 200, $end = '...')

I am using the Laravel 5.2 str_limit() helper function. Is it possible to not break the word?

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
Alexander Kim
  • 17,304
  • 23
  • 100
  • 157
  • 5
    http://stackoverflow.com/questions/79960/how-to-truncate-a-string-in-php-to-the-word-closest-to-a-certain-number-of-chara – Alexey Mezenin Dec 17 '16 at 17:54

2 Answers2

0

Not possible with this function. But you can find interesting information on:

You will have to use the php code on your code or create your own helper.

Community
  • 1
  • 1
Georges O.
  • 972
  • 1
  • 9
  • 21
0

Well if you want to limit the length of a string to a given number of characters without breaking words, then you can use the Php word_wrap function

It returns a certain number of characters of the string. The 'break' argument specifies the character at which the string should be broken. The 'cut' argument specifies that the string should be broken at or before a word.

You can use it as follows:

/** This will add the "@" character after every $character_count characters */
$wrapped_string = word_wrap($string, $character_count, "@", false);
/** This will convert the wrapped string to an array */
$wrapped_string_arr = explode("@", $wrapped_string);
/** The limitted string without breaking word*/
$limitted_string = $wrapped_string_arr[0];
Nadir Latif
  • 3,690
  • 1
  • 15
  • 24