1

我写的脚本有点问题。

它基本上以每种可能的顺序组合了文本的排列。

出于某种原因,它在每个字符之间添加了一个空格 - 任何人都可以找出原因吗?

提前致谢。

<?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> 
'; 
} 




?>
4

1 回答 1

1

除非我弄错了并且我错过了您要执行的操作,否则此脚本的执行方式与您编写的非常相似,只是没有str_replace,并且它会自动为您提供的文件数创建排列(至少 3 个才能工作)适当地) :

$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);

请注意,此版本不使用从文件fopentrim读取的文本来删除内容中的任何空格(CR 和 CL 字符)。

**编辑**

这是我制作的实际测试程序:

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";;

这个脚本的输出是

{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}
于 2012-05-23T03:30:03.287 回答