组合线串成为一个字符串

时间:2014-09-24 08:12:22

标签: php

我做一些文本文件的过程,比如以数字开头的行。

$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

1 个答案:

答案 0 :(得分: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);