Hi,
I have a script that writes variable values to a file, which are passed from checkboxes that the user selects. The result is a document (an .rtf file, actually) that can have any number of these new items. I'm trying to add the correctly incremented numbers. So I want to go from:
<<number>> This is an item you checked from the many.
<<number>> This is another item you chose.
<<number>> Yet another brilliant choice.
to
- This is an item you checked.
- This is another item you chose.
- Yet another brilliant choice.
So this is what my html form looks like:
(Notice I have added "<<number>>" to the beginning of each value. This is what I want to replace with item numbers in my final document. )
<p>Hello. Please check each of the items that you would like to include:</p>
<form action="<?php echo $PHP_SELF; ?>" method=post>
<input name="FOO[]" type="checkbox" value="<<number>> This is the
first FOO item.">This is the first FOO item.<br />
<input name="FOO[]" type="checkbox" value="<<number>> This is the second
FOO item.">This is the second FOO item.<br />
<input name="FOO[]" type="checkbox" value="<<number>> This is the third
FOO item.">This is the third FOO item.<br />
<input name="FOO[]" type="checkbox" value="<<number>> This is the
fourthFOO item.">This is the fourth FOO item.<br />
</form>
Yesterday I was so happy when I finally figured out how to get my values into an rtf file using the following php (the rtf file contains placeholders like <<FOO1>> and <<FOO2>> etc.) :
// open our template file
$filename = 'template.rtf';
$fp = fopen ( $filename, 'r+' );
//read our template into a variable
$output = fread( $fp, filesize( $filename ) );
fclose ( $fp );
//Set shorter variable names
$FOO = $_POST['FOO'];
// replace the place holders in the template with our data from the array
$output = str_replace( '<<FOO1>>', $FOO[0], $output );
$output = str_replace( '<<FOO2>>', $FOO[1], $output );
$output = str_replace( '<<FOO3>>', $FOO[2], $output );
$output = str_replace( '<<FOO4>>', $FOO[3], $output );
So the str_replace function leaves me with the variable values, but they still have the <<number>> placeholder. There may be as many as 20 <<number>> fields in a given document, or as few as 1 or 2. How do I replace the first <<number>> with the numeral "1", the second <<number>> with the numeral "2" and so on?
I first thought to use a for loop to create a variable with the inital value of 1, which is str_replace'd with the first <<number>>, then incremented and replaces the second until there are no more <<number>>s left in the document.
for ($numbers = 1, $numbers <= 20, $numbers++) {
$output = str_replace( '<<number>>', $numbers, $output );
}
But of course this doesn't work, and I can see why. Any ideas about how to replace these placeholders? Thanks!
Chris
nonprofitdesign.org