How to read text and get string to convert php json file

I want to get some line in a text file and then divide the conversion into a 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 to get a string for an array and make a JSON file, possibly as follows:

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

this is my php code

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

Please help me. thank you very much

+4
source share
2 answers

try using regex 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 call the parseFile () function to parse the file, which will return a json string. I'm not sure if you need a json string or an array. You can modify the return statement to return a json string or a two-dimensional array, for example,

  return json_encode($json_data); or return $json_data; 

Note. I have not tested it, try and let me know.

+1
source

quick and dirty ... without regular expression ... provided:

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

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

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

readable non-regression solution (this can be made shorter, but I want to make it obvious)

1.) getting the text from the beginning to the first appearance of the separator 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 separate this part from your line ...

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

2.) getting text between two lines of delimiter

 // 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 discard again what you have already processed

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

3.) .... the same game for packages ... (I leave it here .. you understand the idea)

now that you have $ time, $ input, $ packets ... output JSON as follows:

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

You can transfer this material to a function with parameters startDelimiter, endDelimiter and save some code here. The best solution is to use aka regex regular expressions ... but obviously they are harder to learn and not so easy to debug if they don't work the way you want.

0
source

Source: https://habr.com/ru/post/1482303/


All Articles