John Davidson

How can I Extract values with information from big text file using php

0 comments
Message:


I'm trying to get some data from text file, those data are repeated every random line


I'm trying to get this from text file, and there are alot of 2/6 blocks. i need to get them all, using aloop.


Example data I need to get it.



TRAFFIC MEASUREMENT REPORT PART 2/ 6 VLR MEASUREMENT VLRMEAS

SAMPLE DATE: FROM 2020-10-22 14:00:00:09
TO 2020-10-22 15:00:00:13

REGISTERED SUBSCRIBERS:


LOCATION AREA CODE CURRENT AVERAGE TMS1 TMS2 TMS3
UNKNOWN LAC 2 2 0 0 0
113 75868 75749 0 0 0
115 40627 40732 0 0 0
1102 70422 70489 0 0 0
1105 87075 87067 0 0 0
1117 56150 56180 0 0 0
1118 67175 67444 0 0 0
1120 74385 74381 0 0 0
1121 65034 65060 0 0 0
1122 21636 21407 0 0 0
1217 22944 22856 0 0 0
1218 49054 48993 0 0 0
1318 24283 24066 0 0 0
1418 16366 16350 0 0 0
2118 17812 17674 0 0 0
2120 79644 79489 0 0 0
TOTAL 768477 767939 0 0 0

PART 2/ 6
END OF REPORT

from big file (1/6 ,3/6 ect..).


I tried doing like this


    $lines = file($file,FILE_IGNORE_NEW_LINES);
foreach ($lines as $line){
if(strstr($line,'2/ 6')){
echo $line .'<br>';
}elseif (preg_match('/(\w+)\s+(\d\end\:\report\d)\s+-->/', $line, $match)){
print_r($match);
}
}

I get only one line from each block



TRAFFIC MEASUREMENT REPORT PART 2/ 6 VLR MEASUREMENT VLRMEAS
PART 2/ 6
TRAFFIC MEASUREMENT REPORT PART 2/ 6 VLR MEASUREMENT VLRMEAS
PART 2/ 6
TRAFFIC MEASUREMENT REPORT PART 2/ 6 VLR MEASUREMENT VLRMEAS
PART 2/ 6
TRAFFIC MEASUREMENT REPORT PART 2/ 6 VLR MEASUREMENT VLRMEAS
PART 2/ 6

I hope you get me well, I tried to find any answers but I couldn't find what I want.



My Answer:
';_
}_
}_

This code snippet will search for lines in the text file that contain the string '2/ 6' and then echo those lines. If you want to extract specific data from each block of 2/6, you can modify the code to capture the relevant information. Here's an example of how you can extract the data for each block:

php
$lines = file($file, FILE_IGNORE_NEW_LINES);
$blocks = [];
$currentBlock = [];

foreach ($lines as $line) {
if (strpos($line, '2/ 6') !== false) {
if (!empty($currentBlock)) {
$blocks[] = $currentBlock;
$currentBlock = [];
}
}

if (!empty($currentBlock)) {
$currentBlock[] = $line;
}
}

// Add the last block to the blocks array
if (!empty($currentBlock)) {
$blocks[] = $currentBlock;
}

foreach ($blocks as $block) {
foreach ($block as $line) {
echo $line . '
';
}
echo '
';
}


This code will extract each block of data that starts with '2/ 6' and store it in the `$blocks` array. Then, it will loop through each block and echo out the lines of data within each block. You can modify this code to extract specific information from each block as needed.

Rate this post

3 of 5 based on 6010 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us