PHP脚本在每个字符之间添加一个空格

时间:2012-05-23 02:53:17

标签: php text whitespace

我写的脚本有点问题。

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

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

提前致谢。

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




?>

1 个答案:

答案 0 :(得分:1)

除非我错了,我错过了你想要做的事情,这个脚本的功能与你写的非常相似,只是没有str_replace,它会根据你提供的文件数量自动创建permations (至少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}