1

I have a little problem with a script I wrote.

It basically combines permutations of text in every possible order.

For some reason it is adding a space in between every character - can anyone figure out why?

Thanks in advance.

<?php  


if(isset($_POST['submit'])) 
{ 



 //pull in the perms
$perms = "{#A#B#C|#A#B#D|#A#B#E|#A#B#F|#A#C#D|#A#C#E|#A#C#F|#A#D#E|#A#D#F|#A#E#F|#B#C#D|#B#C#E|#B#C#F|#B#D#E|#B#D#F|#B#E#F|#C#D#E|#C#D#F|#C#E#F|#D#E#F}";

//open the box's
$box1= fopen("box1.txt","r");
$box2= fopen("box2.txt","r");
$box3= fopen("box3.txt","r");
$box4= fopen("box4.txt","r");
$box5= fopen("box5.txt","r");
$box6= fopen("box6.txt","r");

//this is the output
$tulis = fopen("output.txt","w+");

//read the box's
$box1 = fread($box1, filesize("box1.txt"));
$box2 = fread($box2, filesize("box2.txt"));
$box3 = fread($box3, filesize("box3.txt"));
$box4 = fread($box4, filesize("box4.txt"));
$box5 = fread($box5, filesize("box5.txt"));
$box6 = fread($box6, filesize("box6.txt"));


$perms = str_replace("#A","$box1",$perms);
$perms = str_replace("#B","$box2",$perms);
$perms = str_replace("#C","$box3",$perms);
$perms = str_replace("#D","$box4",$perms);
$perms = str_replace("#E","$box5",$perms);
$perms = str_replace("#F","$box6",$perms);

echo $text;

fwrite($tulis,$perms);


//close them properly
$box1= fopen("box1.txt","r");
$box2= fopen("box2.txt","r");
$box3= fopen("box3.txt","r");
$box4= fopen("box4.txt","r");
$box5= fopen("box5.txt","r");
$box6= fopen("box6.txt","r");

fclose($box1);
fclose($box2);
fclose($box3);
fclose($box4);
fclose($box5);
fclose($box6);



fclose($tulis);

} 

//this means that if the submit button hasn't been pressed, print the form! 
else 
{ 
    print ' 
<form action="'.$_SERVER['PHP_SELF'].'" method="POST"> 


<input type="submit" name="submit"></input> 
</form> 
'; 
} 




?>
2
  • what are the contents of the txt files? Can you provide at least a sample of their content? Commented May 23, 2012 at 3:14
  • sure the content is just normal ascii text delimited by brackets and lines such as: {text text|text text|text text|text} Commented May 23, 2012 at 3:15

1 Answer 1

1

Unless I am mistaking and I missed what you are trying to do, this script is doing pretty much like what you wrote, except that there are no str_replace and it creates permuations automatically for the number of files you provide (at least 3 for it to work properly) :

$files = array(
   "box1.txt",
   "box2.txt",
   "box3.txt",
   "box4.txt",
   "box5.txt",
   "box6.txt"
);
$contents = array();

foreach ($files as $index => $filename) {
   $contents[$index] = trim(file_get_contents($filename), " \n\r");
}

$perms = array();
$len = count($contents);
for ($i=0; $i<$len-2; $i++) {
   for ($j=$i+1; $j<$len-1; $j++) {
      for ($k=$j+1; $k<$len; $k++) {
          $perms[] = $contents[$i] . $contents[$j] . $contents[$k];
      }
   }
}

$text = '{' . implode('|', $perms) . '}';

file_put_contents('output.txt', $text);

Note that this version does not use fopen and trim the text read from the files to remove any whitespaces (CR and CL characters too) from the content.

** Edit **

This is the actual test program that I have made :

header('Content-type: text/plain; charset=utf-8');

$contents = array(
   'A', 'B', 'C', 'D', 'E', 'F'
);

// fixed embedded loops method
$perms = array();
$len = count($contents);
for ($i=0; $i<$len-2; $i++) {
   for ($j=$i+1; $j<$len-1; $j++) {
      for ($k=$j+1; $k<$len; $k++) {
          $perms[] = $contents[$i] . $contents[$j] . $contents[$k];
      }
   }
}

$text = '{' . implode('|', $perms) . '}';

echo $text . "\n";


// Recursive method
function permutationRecursive( & $perms, $maxDepth, $contents = array(), $values = array(), $startIndex = 0) {
   for ($i=$startIndex, $count=count($contents)-($maxDepth-1); $i<$count; $i++) {
      array_push($values, $contents[$i]);
      if ($maxDepth > 1) {
         permutationRecursive( $perms, $maxDepth - 1, $contents, $values, $i + 1);
      } else {
         $perms[] = implode($values);
      }
      array_pop($values);
   }
}

$perms = array();
permutationRecursive($perms, 3, $contents);
$text = '{' . implode('|', $perms) . '}';

echo $text . "\n";;

The output of this script is

{ABC|ABD|ABE|ABF|ACD|ACE|ACF|ADE|ADF|AEF|BCD|BCE|BCF|BDE|BDF|BEF|CDE|CDF|CEF|DEF}
{ABC|ABD|ABE|ABF|ACD|ACE|ACF|ADE|ADF|AEF|BCD|BCE|BCF|BDE|BDF|BEF|CDE|CDF|CEF|DEF}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks man - however the output seems to just be "{Array}" (without "") ?
I added my test program. I am not sure why you would get {Array} unless $perms is an array of arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.