473,394 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Adding line breaks to a variable dynamically

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
9 2025
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: dan glenn | last post by:
Hi. I'm finding that if I have text entered into a <textarea ...> </textarea> in a form, and that text has leading blank lines (with no spaces or anything else), that when I retrieve the entered...
5
by: mailbox | last post by:
Did write it down once, but have forgotten !! How is it you should format your ASP code to that when viewing the resulting HTML text from the browsers "view code" it looks nice with line breaks?
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
10
by: David | last post by:
Can anyone give me a quick code snippet (that is standards-based) for adding OPTION tags to a SELECT dynamically. I have no problem doing it in IE but I am kind of new to the whole standards world...
20
by: David | last post by:
I have a one-line script to add an onunload event handler to the body of the document. The script is as follows: document.getElementsByTagName("BODY").onunload=function s() {alert("s")} Now...
11
by: Steven D'Aprano | last post by:
Suppose I create a class with some methods: py> class C: .... def spam(self, x): .... print "spam " * x .... def ham(self, x): .... print "ham * %s" % x .......
7
by: David Meier | last post by:
Hi, I am new to C# and I am facing this small problem: I start a new process using cygwin and I redirect the standard output to a string variable. When I display the string variable in a list...
15
by: pkirk25 | last post by:
I have a file with roughly 1000 lines that I cannot allow the user to read but which I want to use in my project. I thought I could copy it into one huge string variable but that won't work...
8
by: Harris Kosmidis | last post by:
It seems I cannot understand CSS, well. I have donw the following page: http://www.pennias.gr/home.php It's in greek but the context is of no importance. I used a big table to put in my page...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.