Read specific blocks txt php and extract values - php

I'm doing social service, the idea is to automatize a process. I already capture other values using regex, like:
<?php function LeerEncabezado() {
$fh = fopen('RPREGFM_________007_001.txt', 'r') or die('lel'); $file =
str_replace(',', '',
file_get_contents("RPREGFM_________007_001.txt")); $f =
fopen("RPREGFM_________007_001.txt", "w"); fwrite($f, $file);
fclose($f); while (!feof($fh)) {
$line = fgets($fh);
//Intermedio
if (preg_match('/INTERMEDIO (?<cfintermedio>[\w]+.+)/i', $line, $r1)) {
$CFINTERMEDIO = substr($r1['cfintermedio'], 13, 8);
echo "C.F. Intermedio: $CFINTERMEDIO";
echo '<br/>';
}
//Base
if (preg_match('/BASE (?<cfbase>[\w]+.+)/i', $line, $r2)) {
$CFBASE = substr($r2['cfbase'], 13, 8);
echo "C.F. Base: $CFBASE";
echo '<br/>';
}
//kVArh
if (preg_match('/F.P. (?<fp>[\d]+.+)/i', $line, $r3)) {
$anioi = substr($r3['fp'], 0, 5);
echo "kVArh: $anioi";
echo '<br/>';
echo "--------------------------------------</br>";
}
//Base promedio
if (preg_match('/201912 (?<fp>[\d]+.+)/i', $line, $r3)) {
$anioi = substr($r3['fp'], 8, 5); // echo "kVArh: $anioi";
echo '<br/>';
echo "--------------------------------------</br>";
}
} } LeerEncabezado();
fclose($fh);
And it's working until his point, I got to take 3 values from that text block, the last number in INTERM column, BASE and % M$. In this receipt, it would be 1,5,99.98%. I've used strg_replace before for, I guess ill use it for the %. So I can compare those variables with other in the same receipt.
How can I extract those value taking in mind that the 201912 it's going to change taking in consideration the year and the number of rows in MES might change? Pretty much used the blank space and the read the previous line which it's going to be the one where the 3 values are in?
MES TOTAL PUNTA INTERM BASE TOT PTA INT BAS % M$
201901 9 1 7 744 122 622 99.96
201902 8 1 6 672 107 565 99.97
201903 9 1 7 744 115 629 99.97
201904 9 2 7 719 122 597 99.97
201905 10 1 8 744 88 656 99.98
201906 10 1 8 720 80 640 99.97
201907 12 2 10 744 92 652 98.89
201908 13 2 11 744 88 656 97.74
201909 11 1 9 720 80 640 97.05
201910t 1 7 624 76 548 97.56
201910 10 1 120 20 100 98.80
201911 8 1 6 721 115 606 99.20
201912 7 1 5 744 117 627 99.98

Related

querying opening Hours, late night In Laravel

I have a Openinghours Table and I want to query through so that I get the closing time of a Location.
Here is an instance of the OpeningHoursTable and my code current query Code, that dosn't work, however you might get an Idea...
For instance day_id = 3,
is open from 11:30 till 14:30. and from 17:30 till 00:00 next Day.
I Woud like to have 0000 of day 4 outputted
How can I query this?
Database entries:
location_id | day_id | opened | closed
81 1 1130 1430
81 1 1730
81 2 0000
81 3 1130 1430
81 3 1730
81 4 0000
81 4 1130 1430
81 4 1730
81 5 0000
81 5 1130 1430
81 5 1730
81 6 0000
81 6 1130 1430
81 6 1730
81 7 0000
81 7 1130 1430
81 7 1730
81 1 0000
My "idea" of Code in Location.php :
public function closesTodayAt($closeTo = 2000)
{
$thisDay = today()->dayOfWeek + 1;
$time = $this->openinghours()->where('day_id', $thisDay)->where('closed','!=', "")
orWhere()->get();
if ($time->first()->closed < $closeTo ) {
#code
}
if ($time < $closeTo ) {
$nextDay = today()->addDay(1)->dayOfWeek +1;
$time = $this->openinghours()->where('day_id', $thisDay)->where('opened','!=',"")->first()->opened;
}
if ($time === "0000") {
# code...
}
$time = str_split($time,2);
$time = implode(":", $time);
return $time;
}
Here another part of the Database:
94 1 1100
94 2 0100
94 2 1100
94 3 0100
94 3 1100
94 4 0100
94 4 1100
94 5 0100
94 5 1100
94 6 0200
94 6 1100
94 7 0400
94 7 1100
94 1 0400
If I'm reading this right, the closing hours would always be the earliest found hours of the next day, so simply taking the hours for the next day, sorting and taking the first record would get you the closing time:
public function scopeCloses($query)
{
$nextDayOfWeek = now()->addDay()->dayOfWeek;
$hours= $this->openinghours->where('day_id', $nextDayOfWeek)
->sortBy('closes')
->first();
return $hours->closes;
}

Is there a way to remove two numbers not affecting others?

The code is giving the following output and each time, it will give a different output.
Is there any way to remove to 2 numbers for example 15 and 60, so that other numbers are not effected they remain the same?
I have tried but could not get the desired result.
Any help or any idea how to do this?
<?php
$arr = array();
for ($i = 1; $i < 82; $i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
echo "<table>";
foreach ($lines as $key => $line) {
echo "<tr>";
for ($i = 0; $i < sizeof($line); $i++) {
echo "<td align='right'>" . $line[$i] . "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
Out put
52 67 5 44 76 49 1 27 28
73 33 19 66 4 14 63 45 62
26 75 50 80 70 38 12 54 78
9 69 36 32 2 7 56 11 51
40 20 22 15 60 65 31 41 77
57 29 34 79 68 23 18 71 39
42 10 72 17 81 30 35 48 47
37 59 53 6 3 55 13 46 58
61 25 24 64 74 43 21 8 16
This should work for you:
Just place the code before your shuffle() call. Basically first we create an array of patterns, by looping through all numbers, which we want to extract from the list, with array_map().
The pattern basically has two anchors to match the numbers fully in your array and not only partially (E.g. 2 will not match 24, but it will match 2).
With the created pattern array we just use preg_replace() to replace the numbers in your array with an empty string.
$numbers = [15, 60];
$pattern = array_map(function($v){
return "/^$v$/";
}, $numbers);
$arr = preg_replace($pattern, "", $arr);

php specify file content convert

Is it possible to convert specific text file content into a php array ?
For example:
Text file
//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
Return php array
1 => array(
1 => array(5, 7, 3, 83, 83, 83, 1),
2 => array...
),
2 => array(
1 => array(3, ...),
....
and so on, until end then next number group, and also ignore comments lines // or #
I've got another solution:
<?php
header('Content-type: text/plain');
$string = "//group
1
// first id other values
1 5 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 1
end
//group
2
// first id other values
1 3 7 3 83 83 83 1
2 6 7 3 86 83 83 4
3 3 7 3 63 83 83 7
4 3 7 3 84 83 86 2
end";
$string = preg_replace('/[^0-9 \n]/','',$string);
$array = array_filter(explode("\n", $string));
$temp_array = array();
$new_array = array();
$index = -1;
foreach($array as $key => $arr){
if(strlen(trim($arr)) == 1 && intval($arr) > 0){
$index = intval($arr);
}
else if(strlen(trim($arr)) > 4){
$temp_array = array_values(array_filter(explode(" ", $arr)));
$temp_index = $temp_array[0];
unset($temp_array[0]);
$new_array[$index][$temp_index] = $temp_array;
}
}
print_r(array_filter($new_array));
?>
Solved...
Text file
// group 1
1
1 4 3 "ssssssss"
end
// group 2
2
1 5 4 "ssssssss s"
end
// group 3
3
1 6 5 "ssssssss ss"
end
php convert code
$list = array();
$handle = fopen('Item.txt', 'r');
while(($line = fgets($handle)) !== false)
{
// skip comments
if(preg_match('!^//!', $line) || preg_match('/#/', $line))
{
continue;
}
// replace all spaces
$line = preg_replace('~"[^"]*"(*SKIP)(*F)|\s+~', ',', $line);
// skip blanks
if($line[0] == ',')
{
continue;
}
// define group
if(substr_count($line, ',') == 1 || substr_count($line, 'end') == 1)
{
if(substr_count($line, 'end') == 0)
{
$key = str_replace(',', '', $line);
}
continue;
}
// remove last comma
if(substr($line, -1) == ',')
{
$line = substr($line, 0, -1);
}
$arguments = explode(',', $line);
$id = $arguments[0];
unset($arguments[0]);
$list[$key][$id] = $arguments;
}
fclose($handle);
print_r($list);
output http://prntscr.com/6emxpq

PHP: echoing a few selected lines of a txt file

I need to echo only selected lines from a .txt file using PHP.
The txt file content looks like this:
1 111 111 111 111 111 111 111 111 111 111
2 196 182 227 190 195 196 278 197 323 265
3 84.1 84.1 84.1 84.2 85.4 84.1 84.2 84.1 84.1 84.1
4 107 107 107 107 107 107 107 107 107 107
5 10.0 9.29 9.86 9.90 9.57 9.56 9.52 9.55 10.0 9.62
6 0.652 0.622 0.676 0.617 0.637 0.617 0.563 0.601 0.586 0.601
7 132 132 132 132 132 132 481 132 132 132
8 113 113 113 113 113 113 113 113 113 113
9 510 571 604 670 647 667 656 257 264 431
10 245 246 247 246 246 245 247 246 247 247
The previous working code was echoing every lines.
$fp = fopen("http://xxx/myfile.txt","r");
while (!feof($fp)) {
$page .= fgets($fp, 4096);
}
fclose($fp);
echo $page;
break;
I'm trying to get the same formating but only with the selected lines.
For instance let say only lines: 3, 5, 8, 10.
The second step would then be if the lines could be echoed in a different order than the initial one.
If someone knows a simple way to do it, that would be excellent!
Thanx in advance.
$lines = file('http://xxx/myfile.txt');
print $lines[2]; // line 3
print $lines[4]; // line 5
...
see file()
here's an example of how it can be done:
function echo_selected_lines_of_file($file, array $lines)
{
$fContents = file_get_contents($file); // read the file
$fLines = explode("\n", $fContents); // split the lines
foreach($fLines as $key=>$fLine) // foreach line...
if(in_array($key, $lines)) //if its in the $lines array, print it
echo $fLine.'<br>';
}
echo_selected_lines_of_file('index.php', array(1,4,7));
You could use the file() which converts each line into a array. But if your file is big it will consume a bit of memory.
$lines_to_print = [1,3,5,8];
$lines = file('file.txt');
for($i in $line_to_print) {
echo $lines[$i];
}
An efficient solution is
$i = 0;
$lines_to_print = [1,3,5,8];
$file = fopen("file.txt", "r");
while(!feof($file)){
$line = fgets($file);
if(in_array(++$i, $lines_to_print)) {
echo $line
}
}
fclose($file);

Was ending character reached or not

In summary I am using stream_get_line to read a line of a file, replace a string and then write the line to another file.
I am using stream_get_line and supplying the "ending" parameter to instruct the function to read lines, or if there is no new line then read 130 bytes.
What I would like to know is how can I know if the 3rd parameter (PHP_EOL) was found, as I need to write exactly the same line (except for my string replacement) to the new file.
For reference...
string stream_get_line ( resource $handle , int $length [, string $ending ] )
It's mainly needed for the last line, sometimes it will contain a newline character and sometimes it doesn't.
My initial idea is to seek to the last line of the file and search the line for a new line character to see if I need to attach a newline to my edited line or not.
You could try using fgets if the stream is in ASCII mode (which only matters on Windows). That function will include the newline if it is found:
$line = fgets(STDIN, 131);
Otherwise, you could use ftell to see how many bytes were read and thus determine whether there was a line ending. For example, if foo.php contains
<?php
while (!feof(STDIN)) {
$pos = ftell(STDIN);
$line = stream_get_line(STDIN, 74, "\n");
$ended = (bool)(ftell(STDIN) - strlen($line) - $pos);
echo ($ended ? "YES " : "NO ") . $line . "\n";
}
executing echo -ne {1..100} '\n2nd to last line\nlast line' | php foo.php will give this output:
NO 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
NO 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5
NO 3 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
YES 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
YES 2nd to last line
NO last line

Categories