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.
For example:
$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.'; $lines = str_split_whole_word($longString, $x);
Desired output:
$lines = Array( [0] = 'I like apple. You' [1] = 'like oranges. We' [2] = and so on... )
As the name suggests, a Java String Split() method is used to decompose or split the invoking Java String into parts and return the Array. Each part or item of an Array is delimited by the delimiters(“”, “ ”, \\) or regular expression that we have passed. The return type of Split is an Array of type Strings.
Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.
The easiest solution is to use wordwrap()
, and explode()
on the new line, like so:
$array = explode( "\n", wordwrap( $str, $x));
Where $x
is a number of characters to wrap the string on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With