2014-09-24 191 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);