PHP: echoing a few selected lines of a txt file - php

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

Related

Read specific blocks txt php and extract values

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

ASCII converter is showing blank with wrong input - PHP

I'm trying to convert ASCII to characters with the following script.
<?php
$decencrypt = explode(" ", $_POST['decryptinput']);
for($i=0;$i<=count($decencrypt)-1;$i++){
echo(chr($decencrypt[$i]));
}
?>
It works with text like: Lorem Ipsum (Encrypted in ASCII)
But when I use input: for($i=0;$i<=count($decencrypt)-1;$i++){for($i=0;$i<=count($decencrypt)-1;$i++){
(f.e.)
It won't work and my site just shows blank.
Can someone help me out so every possible input will work?
Notice that the input looks like this: 116 101 115 116 32 116 101 120 116 32 102 111 114 32 115 116 97 99 107 111 118 101 114 102 108 111 119 13 10
I think this is what you are looking for.
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$decryptinput = '116 101 115 116 32 116 101 120 116 32 102 111 114 32 115 116 97 99 107 111 118 101 114 102 108 111 119 13 10';
echo $decryptinput.'<br>';
echo asciiDecode($decryptinput);
echo '<br>';
echo asciiEncode("test text for stackoverflow\r\n");
function asciiDecode($iAsciiStr){
$retStr = '';
foreach(explode(' ', $iAsciiStr) as $key=>$val){
$retStr .= chr($val);
}
return $retStr;
}
function asciiEncode($iDecStr){
$retStr = '';
foreach(str_split($iDecStr) as $key=>$val){
$retStr .= ord($val).' ';
}
return $retStr;
}

How to show these numbers in tabular format?

This code is giving the following output, I want to show these numbers in table is there any way to do this? and why it is not showing the numbers in proper format like why 53 is not exactly below 40 and others also not showing in proper order?
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("&nbsp&nbsp&nbsp", $line);
}
echo implode("<br>", $lines);
?>
Output
73 40 79 1 43 7 76 44 18
6 53 45 55 71 20 80 66 74
69 51 52 65 22 63 59 50 54
29 33 23 49 77 24 61 60 58
8 81 30 15 26 32 16 47 31
17 39 4 35 27 11 5 25 68
2 34 72 42 75 46 48 3 38
14 28 37 62 10 78 12 56 13
41 21 19 36 9 64 67 57 70
If you don't want to use tables or CSS. You can use a <pre> tag then use some tabs and newlines
<pre>
<?php
$arr = array();
for ($i=1;$i<82;$i++) {
$arr[] = $i;
}
shuffle($arr);
$lines = array_chunk($arr, 9);
foreach ($lines as $key => $line) {
$lines[$key] = implode("\t", $line);
}
echo implode("\n", $lines);
?>
Fiddle
Note: I didn't really care about your logic for creating those lines while answering this question since its only about formatting. You can trim down some code too.
Output
22 16 66 79 8 41 47 2 80
29 38 76 18 40 46 73 34 45
31 3 62 68 14 33 20 72 67
78 44 42 30 51 77 36 25 48
64 70 21 15 19 9 56 50 65
37 27 4 1 35 74 75 52 32
81 23 10 28 26 59 7 54 11
6 63 5 39 53 12 24 60 49
71 55 17 13 61 69 43 57 58
Now with slightly better code
<pre>
<?php
$numbers=range(1,81);
shuffle($numbers);
$c=0;
foreach($numbers as $n)
{
if($c%9==0)echo "\n";
echo $n."\t";
$c++;
}
?>
modify your code like 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><td align="right">';
echo $lines[$key] = implode('</td><td align="right">', $line);
echo '</td></tr>';
}
echo '</table>';
?>
Output
57 41 48 17 73 76 7 78 12
69 61 39 80 24 58 45 11 70
47 65 33 21 38 4 19 13 46
59 52 63 14 25 3 30 28 77
50 40 68 6 2 29 20 66 26
72 74 34 75 15 36 71 10 60
55 53 1 16 23 42 51 35 62
44 32 43 64 18 8 54 49 5
81 27 31 67 37 22 79 56 9
try 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>";
?>
Your Out put

Finding and removing outliers in PHP

Suppose I sample a selection of database records that return the following numbers:
20.50, 80.30, 70.95, 15.25, 99.97, 85.56, 69.77
Is there an algorithm that can be efficiently implemented in PHP to find the outliers (if there are any) from an array of floats based on how far they deviate from the mean?
Ok let's assume you have your data points in an array like so:
<?php $dataset = array(20.50, 80.30, 70.95, 15.25, 99.97, 85.56, 69.77); ?>
Then you can use the following function (see comments for what is happening) to remove all numbers that fall outside of the mean +/- the standard deviation times a magnitude you set (defaults to 1):
<?php
function remove_outliers($dataset, $magnitude = 1) {
$count = count($dataset);
$mean = array_sum($dataset) / $count; // Calculate the mean
$deviation = sqrt(array_sum(array_map("sd_square", $dataset, array_fill(0, $count, $mean))) / $count) * $magnitude; // Calculate standard deviation and times by magnitude
return array_filter($dataset, function($x) use ($mean, $deviation) { return ($x <= $mean + $deviation && $x >= $mean - $deviation); }); // Return filtered array of values that lie within $mean +- $deviation.
}
function sd_square($x, $mean) {
return pow($x - $mean, 2);
}
?>
For your example this function returns the following with a magnitude of 1:
Array
(
[1] => 80.3
[2] => 70.95
[5] => 85.56
[6] => 69.77
)
For a normally distributed set of data, removes values more than 3 standard deviations from the mean.
<?php
function remove_outliers($array) {
if(count($array) == 0) {
return $array;
}
$ret = array();
$mean = array_sum($array)/count($array);
$stddev = stats_standard_deviation($array);
$outlier = 3 * $stddev;
foreach($array as $a) {
if(!abs($a - $mean) > $outlier) {
$ret[] = $a;
}
}
return $ret;
}
Topic: Detecting local, additive outliers in unordered arrays by walking a small window through the array and calculating the standard deviation for a certain range of values.
Good morning folks,
here is my solution much to late, but since I was looking for detecting outliers via PHP and could'nt find anything basic, I decided somehow smoothing a given dataset in a timeline of 24 h by simply moving a range of 5 items in a row through an unordered array and calculate the local standard deviation to detect the additive outliers.
The first function will simply calculate the average and deviation of a given array, where $col means the column with the values (sorry for the freegrades, this means that in an uncomplete dataset of 5 values you only have 4 freegrades - I don't know the exact english word for Freiheitsgrade):
function analytics_stat ($arr,$col,$freegrades = 0) {
// calculate average called mu
$mu = 0;
foreach ($arr as $row) {
$mu += $row[$col];
}
$mu = $mu / count($arr);
// calculate empiric standard deviation called sigma
$sigma = 0;
foreach ($arr as $row) {
$sigma += pow(($mu - $row[$col]),2);
}
$sigma = sqrt($sigma / (count($arr) - $freegrades));
return [$mu,$sigma];
}
Now its time for the core function, which will move through the given array and create a new array with the result. Margin means the factor to multiply the deviation with, since only one Sigma detects to many outliers, whereas more than 1.7 seems to high:
function analytics_detect_local_outliers ($arr,$col,$range,$margin = 1.0) {
$count = count($arr);
if ($count < $range) return false;
// the initial state of each value is NOT OUTLIER
$arr_result = [];
for ($i = 0;$i < $count;$i++) {
$arr_result[$i] = false;
}
$max = $count - $range + 1;
for ($i = 0;$i < $max;$i++) {
// calculate mu and sigma for current interval
// remember that 5 values will determine the divisor 4 for sigma
// since we only look at a part of the hole data set
$stat = analytics_stat(array_slice($arr,$i,$range),$col,1);
// a value in this interval counts, if it's found outside our defined sigma interval
$range_max = $i + $range;
for ($j = $i;$j < $range_max;$j++) {
if (abs($arr[$j][$col] - $stat[0]) > $margin * $stat[1]) {
$arr_result[$j] = true;
// this would be the place to add a counter to isolate
// real outliers from sudden steps in our data set
}
}
}
return $arr_result;
}
And finally comes the test function with random values in an array with length 24.
As for margin I was curious and choose the Golden Cut PHI = 1.618 ... since I really like this number and some Excel test results have led me to a margin of 1.7, above which outliers very rarelly were detected. The range of 5 is variable, but for me this was enough. So for every 5 values in a row there will be a calculation:
function test_outliers () {
// create 2 dimensional data array with items [hour,value]
$arr = [];
for ($i = 0;$i < 24;$i++) {
$arr[$i] = [$i,rand(0,500)];
}
// set parameter for detection algorithm
$result = [];
$col = 1;
$range = 5;
$margin = 1.618;
$result = analytics_detect_local_outliers ($arr,$col,$range,$margin);
// display results
echo "<p style='font-size:8pt;'>";
for ($i = 0;$i < 24;$i++) {
if ($result[$i]) echo "♦".$arr[$i][1]."♦ "; else echo $arr[$i][1]." ";
}
echo "</p>";
}
After 20 calls of the test function I got these results:
417 140 372 131 449 26 192 222 320 349 94 147 201 ♦342♦ 123 16 15
♦490♦ 78 190 ♦434♦ 27 3 276
379 440 198 135 22 461 208 376 286 ♦73♦ 331 358 341 14 112 190 110 266
350 232 265 ♦63♦ 90 94
228 ♦392♦ 130 134 170 ♦485♦ 17 463 13 326 47 439 430 151 268 172 342
445 477 ♦21♦ 421 440 219 95
88 121 292 255 ♦16♦ 223 244 109 127 231 370 16 93 379 218 87 ♦335♦ 150
84 181 25 280 15 406
85 252 310 122 188 302 ♦13♦ 439 254 414 423 216 456 321 85 61 215 7
297 337 204 210 106 149
345 411 308 360 308 346 ♦451♦ ♦77♦ 16 498 331 160 142 102 ♦496♦ 220
107 143 ♦241♦ 113 82 355 114 452
490 222 412 94 2 ♦480♦ 181 149 41 110 220 ♦477♦ 278 349 73 186 135 181
♦39♦ 136 284 340 165 438
147 311 246 449 396 328 330 280 453 374 214 289 489 185 445 86 426 246
319 ♦30♦ 436 290 384 232
442 302 ♦436♦ 50 114 15 21 93 ♦376♦ 416 439 ♦222♦ 398 237 234 44 102
464 204 421 161 330 396 461
498 320 105 22 281 168 381 216 435 360 19 ♦402♦ 131 128 66 187 291 459
319 433 86 84 325 247
440 491 381 491 ♦22♦ 412 33 273 256 331 79 452 314 485 66 138 116 356
290 190 336 178 298 218
394 439 387 ♦80♦ 463 369 ♦104♦ 388 465 455 ♦246♦ 499 70 431 360 ♦22♦
203 280 241 319 ♦34♦ 238 439 497
485 289 249 ♦416♦ 228 166 217 186 184 ♦356♦ 142 166 26 91 70 ♦466♦ 177
357 298 443 307 387 373 209
338 166 90 122 442 429 499 293 ♦41♦ 159 395 79 307 91 325 91 162 211
85 189 278 251 224 481
77 196 37 326 230 281 ♦73♦ 334 159 490 127 365 37 57 246 26 285 468
228 181 74 ♦455♦ 119 435
328 3 216 149 217 348 65 433 164 473 465 145 341 112 462 396 168 251
351 43 320 123 181 198
216 213 249 219 ♦29♦ 255 100 216 181 233 33 47 344 383 ♦94♦ 323 440
187 79 403 139 382 37 395
366 450 263 160 290 ♦126♦ 304 307 335 396 458 195 171 493 270 434 222
401 38 383 158 355 311 150
402 339 382 97 125 88 300 332 250 ♦86♦ 362 214 448 67 114 ♦354♦ 140 16
♦354♦ 109 0 168 127 89
450 5 232 155 159 264 214 ♦416♦ 51 429 372 230 298 232 251 207 ♦322♦
160 148 206 293 446 111 338
I hope, this will help anyone in the present or future.
Greetings
P.S. To further improve this algorithm you may add a counter, which makes sure, that a certain value must for instance be found at least 2 times, that means in 2 different intervals or windows, before it is labeled as outlier. So a sudden jump of the following values does not make the first value the villain. Let me give you an example:
In 3,6,5,9,37,40,42,51,98,39,33,45 there is an obvious step from 9 to 37 and an isolated value 98. I would like to detect 98, but not 9 or 37.
The first interval 3,6,5,9,37 would detect 37, the second interval 6,5,9,37,40 not. So we would not detect 37, since there is only one problematic interval or one match. Now it should be clear, that 98 counts in 5 intervals and is therefore an outlier. So lets declare a value an outlier, if it "counts" at least 2 times.
Like so often we have to look closely the borders, since they have only one interval, and make for these values an exception.

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