2014-09-24 155 views
0

我做了一些文本文件的過程,如採取以數字開頭的行。組合字符串的行變成一個字符串

$file  = $_FILES['file']['tmp_name']; 
$lines  = file($file); 

foreach ($lines as $line_num => $line) { 
    $checkFirstChar = isNumber($line); 
    if ($checkFirstChar !== false){ 
     $line_parts = explode(' ', $line); 
     $line_number = array_shift($line_parts); 
     $string1 = mysql_real_escape_string(implode(' ', $line_parts)); 
     $string2 = implode(' ', $line_parts); 

     // insert sentence $string1 in database a sentence a row 
     // then I wanna get the text in one string that i've filtered before to do another process 

我用$string2但它仍然串的集合,每個句子是一個字符串,

string(165) "sentence1 . " string(273) "sentence2 . " etc 

所有我需要的是所有的句子再次成爲一個字符串。我能做什麼?感謝

例如輸入文本文件:

========================= 
file : jssksksks 
========================= 
1. blablabla. 
2. bliblibli . 
3. balbalba 

========================= 
file : jkklkok 
========================= 
1.blulbulbu. 
2.bleblelbl 
+0

輸入是什麼樣子? – Ghost 2014-09-24 08:14:46

+0

@Ghost string(165)「sentence1。」string(273)「sentence2。」等 – bruine 2014-09-24 08:16:19

+0

不是我的意思是輸入,文件本身,你評論的是輸出 – Ghost 2014-09-24 08:16:55

回答

1

$ string2爲內循環。您可以收集符合條件的所有行,並且僅在循環後將它們內爆:

$file  = $_FILES['file']['tmp_name']; 
$lines  = file($file); 
newlines = array(); 

foreach ($lines as $line_num => $line) { 
    $checkFirstChar = isNumber($line); 
    if ($checkFirstChar !== false){ 
     $line_parts = explode(' ', $line); 
     $line_number = array_shift($line_parts); 
     // Concat this line together and add it to another array. 
     $newlines[] = implode(' ', $line_parts); 
    } 
} 
// Concat all the lines together. 
$newfile = implode("\n", $newlines);