|
Posted by howarthc@gmail.com on 12/16/06 00:35
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
howarthc@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 <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
Navigation:
[Reply to this message]
|