2012-05-23 267 views
1

我写一个脚本时遇到了一些问题。PHP脚本在每个字符之间添加一个空格

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

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

在此先感谢。

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




?> 
+0

哪些内容txt文件?你能否提供至少一个他们内容的样本? –

+0

确定内容是正常的ascii文本由括号和行分隔,如:{文本文本|文本文本|文本文本|文本} – loveforfire33

回答

1

除非我弄错了,我错过你正在尝试做的,这个脚本是相当做就像你写的东西,但没有str_replace,它为你提供的文件数量会自动创建permuations (至少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} 
+0

感谢人 - 但输出似乎只是“{Array}”(不带“”) ? – loveforfire33

+0

我添加了我的测试程序。我不确定为什么你会得到'{Array}',除非'$ perms'是一个数组数组。 –

相关问题