# Adding line breaks to a variable dynamically

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
OK - here is what I want to do - but I am lost how to do it.

I have a variable

\$mystring = "one two three four five six seven eight nine"

This variable \$mystring can be 4 words long or it could be 50 words
long it is totally variable.  What I want to do is take the number of
words in \$mystring, divide by three and insert a <br> at the end of
each "line".

Example: \$mystring = "one two three four five six seven eight nine"

Convert to:
one two three<br>
four five six<br>
seven eight nine<br>

Example: \$mystring = "one two three four five six seven eight nine ten
eleven twelve"

Convert to:
one two three four<br>
five six seven eight<br>
nine ten eleven twelve<br>

I know I can use explode or strip with a field seperator of " " to
break the variable into component parts - but my non-programmer brain
is confused how I would do this.

Any assistance or pointers would be great... cheers,

Chris

## Re: Adding line breaks to a variable dynamically

OK - I have been thinking about it - I think I worked it out with a
nested while loop - I am not really a programmer so I know this is
messy... so now I have another question.... here is what I have done to
basically achieve my objective:

<?php
old-chap knob";
\$linearray=split("[ ]",\$linestring);

# Set variable to count through the array
\$word="0";

# Set how many lines you want
\$lines="3";
# The number of words in linestring
\$stringlength="12";
# \$linestring divided by \$lines
\$divlength=\$stringlength / \$lines;
\$origdiv=\$divlength;

while (\$lines != 0)
{
while (\$divlength != 0)
{
echo \$linearray[\$word]." ";
\$divlength=\$divlength-1;
\$word=\$word+1;
}

echo "<br>";
\$lines=\$lines-1;
\$divlength=\$origdiv;

}

As you can see - the main problem is that I don't know how to count the
number of words in linestring, I have had to specify stringlength
manually - which is a bit monkey.  Any ideas how display the number of
variables in a php split array?

Chris

howarthc@gmail.com wrote:

## Re: Adding line breaks to a variable dynamically

Please excuse my slightly rude test string :)

howarthc@gmail.com wrote:

## Re: Adding line breaks to a variable dynamically

howarthc@gmail.com wrote:

count();
--
Rik Wasmus

## Re: Adding line breaks to a variable dynamically

howarthc@gmail.com wrote:

Unfortunately, divided by three is not always an integer....
I've taken the liberty to assume "extra's on last row":

\$array = split(' ',\$string);
\$newstring = '';
\$rows = 3;
while(\$rows >= 1){
\$newstring .= implode('
',array_splice(\$array,0,floor(count(\$array)/\$rows--))).'<br>';
}
echo \$newstring;
--
Rik Wasmus

## Re: Adding line breaks to a variable dynamically

Rik wrote:

Make that "bottom up".
Changable to "top down" by replacing floor with ceil.
--
Rik Wasmus

## Re: Adding line breaks to a variable dynamically

yes - I just noticed it also - I fixed it by testing with mod, etc,
etc... overly complex - your solution is neat and tidy - I will try it
now.  Thanks

Chris

Rik wrote:

## Re: Adding line breaks to a variable dynamically

contained the following:

That's a bit complicated for you Rik...

\$array=explode(" ",\$linestring);
for(\$i=0;\$i<count(\$array);\$i++){
echo((\$i+1)%3===0)?\$array[\$i]." <br>\n":\$array[\$i]." ";
}

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

## Re: Adding line breaks to a variable dynamically

Geoff Berrow wrote:

Yup, for some reason I've decided I hate variable counters, don't know why
:-). It doesn't hurt to try different methods either.

It's not three (or #n) words in a row, but an arbitrary amount of words
split in #n rows.

That's a bit more complexity if we want to divide the remainder over the
rest of the rows... Easiest way to do that is to recalculate the number of
words we should use, hence the variable length splice.
--
Rik Wasmus

## Re: Adding line breaks to a variable dynamically

contained the following:

You are right, I didn't read the whole problem.  As usual I prove I am
not worthy... :-(
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/