I'm looking for something along the line of
str_split_whole_word($longString, x)
where $longString
is a collection of sentences, and x
is the character length for each line. It can be fairly long, and I want to basically split it into multiple lines in the form of an array.
So for example,
$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
$lines = str_split_whole_word($longString, x);
$lines = Array(
[0] = 'I like apple. You'
[1] = 'like oranges. We'
[2] = and so on...
)
This solution ensures that the lines will be created without breaking words, what you won't get using wordwrap(). It will use the space to explode the string and then use a foreach to loop the array and create the lines without breaking works and with a maximum length that is defined using
$maxLineLength
. Below is the code, i've done some tests and it works fine.Use
wordwrap()
to insert the linebreaks, thenexplode()
on those linebreaks:The easiest solution is to use
wordwrap()
, andexplode()
on the new line, like so:Where
$x
is a number of characters to wrap the string on.Try This Function.......
Credit: http://www.ebrueggeman.com/blog/abbreviate-text-without-cutting-words-in-half
Made function from Marcio simao comment