Hi,
I am trying to use an array to hold the position of
the a word in a block that is being used ...
There are several words in each block and several blocks in a file.
I want to use each word sequentially from the blocks and write
it to a new file.
Here is an example of what I mean:
Input file:
{Aword1|Aword2|word3} {Bword1|Bword2|Bword3|Bword4} {Cword1|Cword2} {Dword1|Dword2|Dword3|Dword4|Dword5}
Output file 1:
Aword1 Bword1 Cword1 Dword1
Output file 2:
Aword2 Bword2 Cword2 Dword2
Output file 3:
Aword3 Bword3 Cword1 Dword3
Output file 4:
Aword1 Bword4 Cword2 Dword4
Output file 5:
Aword2 Bword1 Cword1 Dword5
Output file 6:
Aword3 Bword2 Cword2 Dword1
As you can see when we get to the last word in the block we need to start again.
This results in a well balanced usage of all words.
( The words can be any collection items that I chose to use)
Here is my code so far:
I input the number of output files I want ... say 10 it is read into $groups
THE FUNCTION
function seq_spin($pass,$file_no){
$last_file_no = $file_no-1;
$mytext = $pass;
$block_no = 1; // block 1
$word_no = -1; // so that the first word will be position 0
$blocks[] = array('file' => $file_no, 'block' =>$block_no, 'word' =>$word_no); // add an element to the array
while(inStr("}",$mytext)){ // continues while text contains an end bracket - }
$rbracket = strpos($mytext,"}",0); // finds position of end bracket - } SO IN FIRST RUN THIS WILL BE: {for|for the purpose of}
$tString = substr($mytext,0,$rbracket); // Grabs the text between position 0 and position of end bracket. SO: [summary]##{for|for the purpose of}
$tStringToken = explode("{",$tString); // makes array splitting at opening bracket. SO: "for|for the purpose of"
$tStringCount = count($tStringToken) - 1; // Number of items in array ( the blocks ). In this case 1 - 1 = 0
$tString = $tStringToken[$tStringCount]; // Grabs the block "for|for the purpose of"
$tStringToken = explode("|",$tString); // Create another array splitting at the "|"
$tStringCount = count($tStringToken); //Number of words = 2
if($file_no == 0){$i = 0;}
else{
$new_word_no = array_search('green', $blocks) +1; // THIS MUST SEARCH THE ARRAY
}
LINE 80 if( $array['key3'] == 'new value' > $tStringCount ) {$new_word_no = 0;} // IF AT THE END OF THE BLOCK, SRART AGAIN
$blocks['word'] = $new_word_no; // UPDATE THEARAY WITH THE NEW NUMBER
$i = $new_word_no; // select based on last word count + 1
$replace = $tStringToken[$i]; // Grab the selected word ... say "for"
$tString = "{".$tString."}"; // put brackets on the block: {for|for the purpose of}
$mytext = str_replaceFirst($tString,$replace,$mytext); // replace "{for|for the purpose of}" with "for"
$block_no=$block_no+1;
}
return $mytext;
}
THE LOOP:
$blocks = array();
for ($i = 0; $i < $groups; ++$i) {
$filename = "{$i}_seq.txt";
$filename = $seqoutpath.$filename;
$Db_text = str_replace("'","`",$Db_text);
$Db_text = preg_replace('/\s\s+/', ' ', $Db_text); // strips excess whitespace
$file_no = $i;
$selected_text = seq_spin($Db_text,$file_no); // This calls the seq_spin function
echo "<br>Created File: $filename<br>$output<br>";
}
I have put many comments on the function lines, I hope it is all readable.
The error I get is :
Undefined variable: array in test.php on line 80
Any help would be much appreciated, I think I have some logical errors !
Thanks