Php split string by length and space

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

Php split string by length and space

mickmackusa

39k11 gold badges76 silver badges112 bronze badges

asked Jun 29, 2012 at 0:44

Php split string by length and space

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.

answered Jun 29, 2012 at 0:46

nickbnickb

58.5k12 gold badges102 silver badges141 bronze badges

2

This code avoid breaking words, you won't get it using wordwrap().

The maximum length is defined using $maxLineLength. I've done some tests and it works fine.

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';

$words = explode(' ', $longString);

$maxLineLength = 18;

$currentLength = 0;
$index = 0;

foreach ($words as $word) {
    // +1 because the word will receive back the space in the end that it loses in explode()
    $wordLength = strlen($word) + 1;

    if (($currentLength + $wordLength) <= $maxLineLength) {
        $output[$index] .= $word . ' ';
        $currentLength += $wordLength;
    } else {
        $index += 1;
        $currentLength = $wordLength;
        $output[$index] = $word;
    }
}

answered Jun 29, 2012 at 4:39

Marcio MazzucatoMarcio Mazzucato

8,4836 gold badges63 silver badges76 bronze badges

2

Use wordwrap() to insert the linebreaks, then explode() on those linebreaks:

// Wrap at 15 characters
$x = 15;
$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
$lines = explode("\n", wordwrap($longString, $x));

var_dump($lines);
array(6) {
  [0]=>
  string(13) "I like apple."
  [1]=>
  string(8) "You like"
  [2]=>
  string(11) "oranges. We"
  [3]=>
  string(13) "like fruit. I"
  [4]=>
  string(10) "like meat,"
  [5]=>
  string(5) "also."
}

answered Jun 29, 2012 at 0:46

Michael BerkowskiMichael Berkowski

263k45 gold badges434 silver badges380 bronze badges

0

My requirements were split text string after every 20 characters without break words. Use wordwrap() to insert the linebreaks after every 20 characters So here is how I do it. hope this is helpful for others who find it this type of solution.

$charactersLimit = 20;
$yourTextString = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.';
$output = explode("\n", wordwrap($yourTextString, $charactersLimit));

Output like this:

Array
(
    [0] => Lorem ipsum dolor
    [1] => sit amet,
    [2] => consectetuer
    [3] => adipiscing elit.
)

answered Sep 30, 2020 at 7:54

Php split string by length and space

This whole task can be accomplished with just one preg_ function call.

  1. Match any character between zero and $maxLength times.
  2. Forget/Release the matched characters from #1 with \K.
  3. Match the next one or more whitespace characters or the end of the string. The characters/position matched here will be consumed in the splitting process and will not appear in the output array.
  4. Set the preg_ function to exclude the empty element that is produced by splitting on the end of string position with PREG_SPLIT_NO_EMPTY.

Code: (Demo)

$longString = 'I like apple. You like oranges. We like fruit. I like meat, also.';
$maxLength = 18;

var_export(
    preg_split("/.{0,{$maxLength}}\K(?:\s+|$)/", $longString, 0, PREG_SPLIT_NO_EMPTY)
);

Output:

array (
  0 => 'I like apple. You',
  1 => 'like oranges. We',
  2 => 'like fruit. I like',
  3 => 'meat, also.',
)

If your input string might contain newlines, then just add the s pattern modifier.
/.{0,{$maxLength}}\K(?:\s+|$)/s (Demo)

answered Sep 25, 2020 at 7:00

Php split string by length and space

mickmackusamickmackusa

39k11 gold badges76 silver badges112 bronze badges

How do you seperate a string by space in PHP?

To split a string into words in PHP, use explode() function with space as delimiter. The explode() function returns an array containing words as elements of the array.

What is split () in PHP?

The split() function will divide a string into various elements, the boundaries of each element based on the occurrence of pattern in string.

How do I separate comma separated values in PHP?

The task is to split the given string with comma delimiter and store the result in an array. Use explode() or preg_split() function to split the string in php with given delimiter. PHP | explode() Function: The explode() function is an inbuilt function in PHP which is used to split a string in different strings.

What is implode in PHP?

The implode() is a builtin function in PHP and is used to join the elements of an array. implode() is an alias for PHP | join() function and works exactly same as that of join() function. If we have an array of elements, we can use the implode() function to join them all to form one string.