如何读取文本并获取字符串以转换JSON文件php

I want to get some string in text file and divide then converting to JSON file this is my text file, data.txt

"5 minute input rate 134000 bits/sec, 164 packets/sec

5 minute output rate 1320000 bits/sec, 150 packets/sec"

I want get a string to array and make a JSON file maybe like this:

$time= "5 minute "
$input= "input rate 134000 bits/sec"
$output= "output rate 1320000 bits/sec"
$array[koneksi]= json_decode('[.".$time.":".$input.]');

this is my code in PHP

<?php
$lines = file('var/www/datakom/data.txt');
$title = 'data';
    foreach ($lines as $line_num => $line) {
        echo htmlspecialchars($line) . "<br />
"; }
?>

Please help me. thanks a lot

try using a regex sth like this :

<?php

//helpfull function
function preg_grep_keys( $pattern, $input, $flags = 0 )
{
    $keys = preg_grep( $pattern, array_keys( $input ), $flags );
    $vals = array();
    foreach ( $keys as $key )
    {
        $vals[$key] = $input[$key];
    }
    return $vals;
}

function parseFile(){
      $pattern='/(?<time>\d+)\s+(?<time_unit>\w+)\s+(?<direction>\w+)\s+(.*?)(?<rate>\d+)\s+(?<rate_unit>\w+)(.*?)(?<packets>\d+)/i';
       $lines = file('var/www/datakom/data.txt');
       $title = 'data';
       $json_data=array();
       foreach ($lines as $line_num => $line) {
         preg_match($pattern,$line,$result);
         $json_data[]=preg_grep_keys('/time|time_unit|direction|rate|rate_unit|packets/',$result);
       }

    return json_encode($json_data);
}

?>

you need to invoke parseFile() function to parse the file , which will return a json "string". I am not sure you need json string or array . You can change the return statement to return json string or two-dimentional array e.g

 return json_encode($json_data);

 or 

 return  $json_data;

Note: I have not tested it , please give it a try and let me know

quick & dirty ... without regex... assuming:

$out = "5 minute input rate 134000 bits/sec, 164 packets/sec" 

is the string you want to create an array from in the following form:

array(
   'time'  => '5',
   'input' => '134000',
   'packets'=> '164'
);

a readable non-regex solution ( this can be done shorter but i want to make it obvious )

1.) getting the text from beginning to the first occurence of a delimiter string:

// find time time interval
$timeDelimiter =  " minute"               // set the delimiter
$timeEnd = strpos($out, $timeDelimiter);  // find the first occurence of delimiter
$time = substr($out, 0, $timeEnd));       // set time to substring before delimiter

now strip this part away from your string ...

$out = substr($out, $timeEnd + strlen($timeDelimiter)); // strip the processed part from $out 

2.) getting the text between two delimiter strings

// find the input interval
$inputDelimiterStart = "input rate ";
$inputDelimiterEnd   = " bits/sec";
$inputStartPos       = strpos($out,  $inputDelimiterStart) + strlen($inputDelimiterStart); 
$inputEndPos         = strpos($out, $inputEndDelimiter);
$input               = substr($out, $inputStartPos, $inputEndPos);

then again strip away what you have already processed

$out = substr($out, $inputEndPos + strlen($inputDelimiterEnd));

3.) .... same game for the packets... ( im leaving it out here .. you get the idea )

now that you have $time, $input, $packets ... output the JSON like this:

echo json_encode(
    array(
         'time'    => $time,
         'input'   => $input,
         'packets' => $packets,
    )
);

You could wrap this stuff into a function with startDelimiter, endDelimiter arguments aswell and save some code here. The best solution is using regular expressions aka regex ... but obviously they are harder to learn and not that easy to debug if they don't work the way you want.