Thanks guys for your help. I've been away for 2 weeks so I am only looking at this now.
dp777, thanks very much for your idea, it seems the right way for me to go. I have tried to implement your idea and I am sort of getting there but my loop seems to go on for too long. I have adapted your idea to the code below:
<?php
$sent = "On the first day of Christmas my true love gave to me, a partridge in a pear tree. On the second day of Christmas my true love gave to me, two turtle doves and a partridge in a pear tree. On the third day of Christmas my true love gave to me, three french hens, two turtle doves and a partridge in a pear tree. On the fourth day of Christmas my true love gave to me, four calling birds, three french hens, two turtle doves and a partridge in a pear tree.";
$leng = 50;
$stringlength = strlen($sent);
if($stringlength > $leng){
$explodeArray = explode(" ", $sent);
}
$i=0;
$stringfloat = 0;
$tot = 0;
while($tot < $stringlength){
$stringfloat = 0;
while($stringfloat < $leng){
echo $explodeArray[$i]." ";
$i++;
$stringfloat = $stringfloat + (strlen($explodeArray[$i]) +1);
$totfloat = $stringfloat;
}
$tot = $tot + $totfloat;
echo "<br />";
}
?>
Below is the output it gives back, as you can see it seems to loop too far. Can anybody see where my code is incorrect?
On the first day of Christmas my true love gave to
me, a partridge in a pear tree. On the second day of
Christmas my true love gave to me, two turtle doves and a
partridge in a pear tree. On the third day of Christmas my
true love gave to me, three french hens, two turtle
doves and a partridge in a pear tree. On the fourth
day of Christmas my true love gave to me, four
calling birds, three french hens, two turtle doves and a
partridge in a pear tree.
Notice: Undefined offset: 93 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21
Notice: Undefined offset: 93 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19
Notice: Undefined offset: 94 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21
Notice: Undefined offset: 94 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19
Notice: Undefined offset: 95 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21
Notice: Undefined offset: 95 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19
The undefined offset runs right up to 126 but I can't understand why my loop goes on for so long. It looks like it does its not actually doing its job properly on the breaking of the lines. Some lines in the paragraph above look like they could have more words on and don't reach the limit of 50.
To reinterate, I am trying to break up a large string so that I can eventually get each line into a new string. The first stage I'm trying get past is seperating the string out onto seperate lines based on a character width of 50 for each line.
Any help would be much appreciated. Thank you.
T12