Abela
Abela

Reputation: 1233

string to array, max length, break on words

I have a string that I would like to break into an array.

Each array needs to have a set length, let us say, 45 characters.

Each break point needs to be based on words and not characters (so, never more than 45, but could be in the 30'ish range, if a long-char word is used)

So, let us take this string

$content = 'Sed a suscipit enim in consectetur lacus Vestibulum efficitur convallis luctus Curabitur vehicula a massa nec pretium Sed a maximus nunc in aliquam orci Nulla facilisi Nullam vulputate ornare dictum Sed fermentum sapien nec felis gravida eget consequat ex iaculis Vestibulum nec feugiat nisl sed consequat nulla Sed in odio congue dictum augue et mollis nunc Integer libero leo eleifend sed congue ut ultrices sed nulla Integer sapien felis sollicitudin et turpis nec mattis egestas augue Phasellus non lectus ac ipsum ultrices consectetur eu at justo Nam';

And a resulting array (what it is that I am after)

    $data = [];

    $data[] = 'Sed a suscipit enim in consectetur lacus';
    $data[] = 'Vestibulum efficitur convallis luctus';
    $data[] = 'Curabitur vehicula a massa nec pretium Sed a';
    $data[] = 'maximus nunc in aliquam orci Nulla facilisi';
    $data[] = 'Nullam vulputate ornare dictum Sed fermentum';
    $data[] = 'sapien nec felis gravida eget consequat ex';
    $data[] = 'iaculis Vestibulum nec feugiat nisl sed';
    $data[] = 'consequat nulla Sed in odio congue dictum';
    $data[] = 'augue et mollis nunc Integer libero leo';
    $data[] = 'eleifend sed congue ut ultrices sed nulla';
    $data[] = 'Integer sapien felis sollicitudin et turpis';
    $data[] = 'nec mattis egestas augue Phasellus non lectus';
    $data[] = 'ac ipsum ultrices consectetur eu at justo Nam';

Each array is under 45 characters and based on word length not hard-numbers.

I know that we can use this code to break a string into a specific length, based on word and not chars, but not sure how to really make this all work together.

$content = substr($content, 0, strpos($content, ' ', 45));

Upvotes: 1

Views: 287

Answers (2)

u_mulder
u_mulder

Reputation: 54841

In your case you can use wordwrap function and then explode or preg_split

First we need to split your string into pieces of required length:

$ps = wordwrap($content, 45);

By default the divider will be \n. You can set your own:

$ps = wordwrap($content, 45, '===');    // === is your divider    

Next, if you need array you should explode or preg_split your $ps variable. If you use custom divider (like ===) you can:

$data = explode('===', $ps);

With \n as divider use preg_split:

$data = preg_split('/\n/', $ps);

So, final code can be:

$data = preg_split('/\n/', wordwrap($content, 45));

Upvotes: 2

Subin Thomas
Subin Thomas

Reputation: 1416

This is splitting your string with positon of space before 45th character.

$length = strlen($content);
$startpos = 0;
$out = array();
while($startpos<$length-45)
    {
    for ($i = $startpos +45 ; $i > $startpos; $i--)
    {
        if($content[$i] == " ")
            {
            array_push($out,substr($content,$startpos ,$i-$startpos));
            $startpos = $i+1;
            break;
            }
    }
}
array_push($out,substr($content,$startpos ));
var_dump($out);

Upvotes: 0

Related Questions