By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,974 Members | 1,834 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,974 IT Pros & Developers. It's quick & easy.

Adding line breaks to a variable dynamically

P: n/a
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 <brat 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

Dec 16 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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
$linestring="test arse bottom ass willy bum snake wadger tadger winkle
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
ho******@gmail.com wrote:
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 <brat 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
Dec 16 '06 #2

P: n/a
Please excuse my slightly rude test string :)

ho******@gmail.com wrote:
$linestring="test arse bottom ass willy bum snake wadger tadger winkle
Dec 16 '06 #3

P: n/a
Rik
ho******@gmail.com wrote:
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 <brat 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.
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
Dec 16 '06 #4

P: n/a
Rik
ho******@gmail.com wrote:
Any ideas how display the number of
variables in a php split array?

count();
--
Rik Wasmus
Dec 16 '06 #5

P: n/a
Rik
Rik wrote:
I've taken the liberty to assume "extra's on last row":

Make that "bottom up".
Changable to "top down" by replacing floor with ceil.
--
Rik Wasmus
Dec 16 '06 #6

P: n/a
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:
ho******@gmail.com wrote:
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 <brat 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.

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
Dec 16 '06 #7

P: n/a
Message-ID: <3e**************************@news2.tudelft.nlfr om Rik
contained the following:
>$array = split(' ',$string);
$newstring = '';
$rows = 3;
while($rows >= 1){
$newstring .= implode('
',array_splice($array,0,floor(count($array)/$rows--))).'<br>';
}
echo $newstring;
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/
Dec 16 '06 #8

P: n/a
Rik
Geoff Berrow wrote:
Message-ID: <3e**************************@news2.tudelft.nlfr om Rik
contained the following:
>$array = split(' ',$string);
$newstring = '';
$rows = 3;
while($rows >= 1){
$newstring .= implode('
',array_splice($array,0,floor(count($array)/$rows--))).'<br>';
}
echo $newstring;

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]." ";
}
Yup, for some reason I've decided I hate variable counters, don't know why
:-). It doesn't hurt to try different methods either.

Then again, your answer is flawed :P

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
Dec 16 '06 #9

P: n/a
Message-ID: <6d*************************@news2.tudelft.nlfro m Rik
contained the following:
>$array=explode(" ",$linestring);
for($i=0;$i<count($array);$i++){
echo(($i+1)%3===0)?$array[$i]." <br>\n":$array[$i]." ";
}

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

Then again, your answer is flawed :P

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

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/
Dec 16 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.