Forum Moderators: coopster
This is just an example. I know I could number 42 lines by hand, but I'd like an automated system to do it for much more line numbers.
Then in a foreach or while loop, increment $x as needed.
$x = 1;
foreach ($array as $key=>$val) {
echo "<br>$val $x";
$x ++;
}
If you need $x to start at #1 for the second sonnet, then you can substitute $x ++; with this:
if ($x == '14') $x = 1;
else $x ++;
A dreary day for love and lovers - 1
this cold and wet Valentines Day; - 2
Sitting alone, glancing at each other, - 3
both hoping the other will not go away. -4
The banners and signs all bright and pink, - 5
they're supposed to make us feel better; -6
As I sit here alone, I wonder, I think, -7
just how much of this really matters. - 8
When I looked up, looked over at you, - 9
you looked quickly the other way; - 10
To all the lovers, there must be so few, - 11
I wish a Happy Valentine's Day. - 12
Sorry, temptation won over
[us3.php.net...]
php -R 'echo($argn." - ".($i++%14+1)."\n");' < test.txt
Your move ;)
The above PHP command line hack is PHP5. However, even within the code based examples above the modulus operator (%) is something you can use whenever you want to generate a repetative count from x to y.
Modulus means "the remainder following division", so by incrementing a counter "MOD 14" you get values in the range 0-13 (which is why you see the +1 in the above hacks). Your problem could be solved something like this (assuming $infile as the source text file opened for reading, and $outfile as your output, opened for writing)...
while(!feof($infile))
{
$line = fgets($infile,1024);
fputs($outfile,$line." - ".($i++%14+1)."\n");
}