bruine
bruine

Reputation: 647

combine string of lines become one string

I do some process of text files like take the lines that begin with number.

$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

I use $string2 but it still the collection of string, every sentence is a string,

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

all i need is all of sentence become one string again. what could I do? thanks

example input text file :

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

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

Upvotes: 0

Views: 53

Answers (1)

GolezTrol
GolezTrol

Reputation: 116100

$string2 is inside the loop. You can collect all the lines that match the criteria and only implode them after the loop:

$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);

Upvotes: 1

Related Questions