php - get specific column from txt file - columns separated irregularly - php

I'm trying to read data to php from a txt file, I already was able to do this, but now it's kind of different.
the data file consists of rows looking like this:
10.09.16 0:05 16.7 16.8 16.7 70 11.2 3.2 ENE 0.27 6.4 ENE 16.7 16.4 16.4 --- 946.9 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000 19.8 44 7.2 18.7 8.35 1.1146 15.6 0.00 112 2 100.0 5
10.09.16 0:10 16.7 16.8 16.7 70 11.2 4.8 E 0.40 6.4 E 16.7 16.4 16.4 --- 946.8 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000 19.7 43 6.8 18.6 8.25 1.1151 15.6 0.00 115 2 100.0 5
as you see, the sepparation between the columns is not always the same, not for every column, and not even between rows.
now I have to get data from some specific columns.
can someone help me with this?
thanks!

You can read the file line by line, and then replace all the space by just one space (or other separator).
Then explode the line with the separator to get an array with each column.
Resource you could use:
Read file: check this answer
Replace space using str-replace
Explode in array using explode
Also, you could replace the space by some ; and use fgetcsv

Regular expressions are very powerful when it comes to matching and separating text sequences:
Simple approach if you don't know the number of columns in advance:
<?php
$input = <<<EOT
10.09.16 0:05 16.7 16.8 16.7 70 11.2 3.2 ENE 0.27 6.4 ENE 16.7 16.4 16.4 --- 946.9 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000
10.09.16 0:10 16.7 16.8 16.7 70 11.2 4.8 E 0.40 6.4 E 16.7 16.4 16.4 --- 946.8 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000
EOT;
$output = [];
foreach(explode("\n", $input) as $subject) {
if (trim($subject)) {
preg_match_all('/(?:([^\s]+)\s+)+/uU', $subject, $tokens);
$output[] = $tokens[1];
}
}
var_dump($output);
Much more elegant approach if you do know the number of columns in advance:
<?php
$input = <<<EOT
10.09.16 0:05 16.7 16.8 16.7 70 11.2 3.2 ENE 0.27 6.4 ENE 16.7 16.4 16.4 --- 946.9 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000
10.09.16 0:10 16.7 16.8 16.7 70 11.2 4.8 E 0.40 6.4 E 16.7 16.4 16.4 --- 946.8 0.00 0.0 0 0.00 0 0.0 0.00 0.0 0.006 0.000
EOT;
preg_match_all('/(?:([^\s]+)\s+)+/uUm', $input, $output);
$output = array_chunk($output[1], 39);
var_dump($output);
I declared the input text inline to reduce complexity, you can read it from a file instead.
Both approaches will produce an identical output alone these lines:
array(2) {
[0] =>
array(39) {
[0] =>
string(8) "10.09.16"
[1] =>
string(4) "0:05"
[2] =>
...
[37] =>
string(5) "100.0"
[38] =>
string(1) "5"
}
[1] =>
array(39) {
[0] =>
string(8) "10.09.16"
[1] =>
string(4) "0:10"
[2] =>
...
[37] =>
string(5) "100.0"
[38] =>
string(1) "5"
}
}

Related

php exec() return wrong data

When I execute php script with exec('vnstat -tr', $results);
Output:
Array ( [0] => Sampling eth0 (5 seconds average)... 0 packets sampled in 5 seconds
[1] => Traffic average for eth0
[2] =>
[3] => rx 0.00 kbit/s 0 packets/s
[4] => tx
0.00 kbit/s 0 packets/s
[5] => )
When I execute vnstat -tr in console.
Output look like this:
root#s2:~# vnstat -tr 313727 packets sampled in 5 seconds Traffic average for eth0
rx 10,42 Mbit/s 20092 packets/s
tx 488,22 Mbit/s 42653 packets/s
Someone Could tell my why PHP returned wrong data?

php - converting badly formatted txt to csv

i have a badly formatted text file which i would like to convert to csv.
Here's an example:
100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO
160032 NA/1-2014-158129 PAOLO GORINI SNC LODI MAG 6 2014 11:51AM DANNEGGIATO -10% 2 0 2 0 NO
54900 NA/1-2014-158070 STRADA VECCHIA CREMONESE SNC LODI MAG 6 2014 9:53AM DANNEGGIATO +10% 10 0 10 0 NO
100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
147959 NA/1-2014-146140 DOSSENA SNC LODI GEN 3 2014 10:45AM DANNEGGIATO -10% 200 0 200 0 NO
That is roughly in this form :
[number] [id] [awfully formatted street] ['LODI'] [timestamp] [damaged or not] [percentage] [squaremeters] [squaremeters] [squaremeters] [squaremeters] [asbest-crumbled or not]
My problem is how to extract the 3rd part, [awfully formatted street].
Basically it's the string after [id] preceding the string ['LODI'] (but ['LODI'] must be just before [timestamp] )
Should i explode() each line by spaces and then traversing the array backwards, overtake [timestamp], overtake ['LODI'] and joining the values before array[id], i.e array [1]? Or is there a smarter (elegant) way to do this, perhaps with preg_match()?
Thanks for any hint!
<?php
// read file line by line
$line = '148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO';
//start by seperating the string on LODI
$lodi_split = explode('LODI', $line);
// Now split the first occ into an array on space
$bits = explode(' ', $lodi_split[0]);
$address = '';
// start reading occurance from occ 2 to loose the first 2 fields
for ($i=2; $i < count($bits); $i++ ) {
$address .= $bits[$i] . ' ';
}
echo $address . PHP_EOL;
Result is
CAVALLOTTI SNC
This should work to extract the address from a row.
<?php
$row = "100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO";
$row_array = preg_split('/\s+/', $row);
array_shift($row_array);
array_shift($row_array);
for($i=0; $i<12; $i++){
array_pop($row_array);
}
$address = implode(" ", $row_array);
?>
I think explode won't do here. I propose using regexp. For Instance, If you read your .txt file as one string(where data strings are separated with \n):
$f = fopen($fname="file.txt", "rt");
$str = fread($f, filesize($fname)));
fclose($f);
Then use preg_match_all() like this:
$re = "/^(\\d+)\\s*(.*)(LODI)\\s*(.+(?:AM|PM))\\s*(\\w+)\\s+(-?\\d{1,3}%)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\w+)$/m";
preg_match_all($re, $str, $matches,PREG_SET_ORDER );
echo "<pre>\n";
print_r($matches);
echo "</pre>\n";
The output would look like this:
Array
(
[0] => Array
(
[0] => 100910 NA/1-2013-99636 VIA DEI PESCATORI 2/A LODI APR 8 2013 4:24PM DANNEGGIATO -10% 200 2700 0 0 NO
[1] => 100910
[2] => NA/1-2013-99636 VIA DEI PESCATORI 2/A
[3] => LODI
[4] => APR 8 2013 4:24PM
[5] => DANNEGGIATO
[6] => -10%
[7] => 200
[8] => 2700
[9] => 0
[10] => 0
[11] => NO
)
[1] => Array
(
[0] => 148013 NA/1-2014-146194 CAVALLOTTI SNC LODI GEN 3 2014 3:37PM DANNEGGIATO -10% 0 0 2 0 NO
[1] => 148013
[2] => NA/1-2014-146194 CAVALLOTTI SNC
[3] => LODI
[4] => GEN 3 2014 3:37PM
[5] => DANNEGGIATO
[6] => -10%
[7] => 0
[8] => 0
[9] => 2
[10] => 0
[11] => NO
)
..........// And so on
I used the text that you provided above in this example. So in the output you recieve your data formated as list of arrays. So you can do whatever you want with it. $matches[$i][0] - will store the whole match so just skip it and use $matches[$i][1]....$matches[$i][11] as your data.

symfony 2 & sonata admin debug toolbar broken, failed, and display blank

Sometimes I click the (symfony2 & the back-end I use sonata admin) debug toolbar button it display blank:
display nothing(check the image here)
Here is source of Firefox , it's broken here(in a svg tag):
</div>
<div id="content">
<div id="header" class="clear-fix">
<h1>
<svg width="275" height="62" xmlns="http://www.w3.org/2000/svg" version="1.1" x="0px" y="0px" viewBox="0 0 275 62" enable-background="new 0 0 275 62" xml:space="preserve"><path fill="#010202" d="M31 0C13.9 0 0 13.9 0 31c0 17.1 13.9 31 31 31c17.1 0 31-13.9 31-31C62 13.9 48.1 0 31 0z M47.7 17.9 c-1.4 0-2.4-0.8-2.5-2.1c0-0.5 0.1-0.9 0.4-1.4c0.3-0.6 0.4-0.7 0.4-1c0-0.8-1.3-0.9-1.6-0.8c-4.7 0.2-5.9 6.4-6.9 11.5l-0.5 2.7 c2.7 0.4 4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6 0 4.7-3.3 5.7-6.1l5.5-15.2L179.4 25.4L179.4 25.4z"/></g><g><path fill="#ADADAD" d="M185.7 46.4V25.1h2.4v2c0.6-0.8 1.2-1.4 1.9-1.8c0.7-0.4 1.6-0.6 2.6-0.6c1.3 0 2.5 0.3 3.5 1 c1 0.7 1.8 1.6 2.3 2.9c0.5 1.2 0.8 2.6 0.8 4.1c0 1.6-0.3 3-0.8 4.3c-0.6 1.3-1.4 2.2-2.5 2.9c-1.1 0.7-2.2 1-3.4 1 c-0.9 0-1.7-0.2-2.3-0.6c-0.7-0.4-1.3-0.8-1.7-1.4v7.5H185.7z M188.1 32.8c0 2 0.4 3.4 1.2 4.4s1.8 1.4 2.9 1.4 c1.2 0 2.2-0.5 3-1.5c0.8-1 1.2-2.5 1.2-4.6c0-2-0.4-3.4-1.2-4.4c-0.8-1-1.8-1.5-2.9-1.5c-1.1 0-2.1 0.5-3 1.6 C188.5 29.4 188.1 30.9 188.1 32.8z"/><path fill="#ADADAD" d="M202.2 40.5V25.1h2.4v2.3c0.6-1.1 1.2-1.8 1.7-2.2c0.5-0.3 1.1-0.5 1.7-0.5c0.9 0 1.8 0.3 2.7 0.8l-0.9 2.4 c-0.6-0.4-1.3-0.6-1.9-0.6c-0.6 0-1.1 0.2-1.5 0.5c-0.5 0.3
In a svg tag(check the image here)
It's make me confused for a long time. Who can tell me why?

How can I monitor memory consumption during webserver stress test?

I want to do a stress test on an Apache webserver that I have running on localhost. The test will request the webserver to execute a PHP application that I wrote. I want to see how much memory (RAM) the webserver (and/or the associated PHP process) consumes during the test. Or to see how much it consumed after the test is done.
My OS is Ubuntu 13.10.
I looked at Apache Bench, Apache JMeter, Siege and httperf. None of them seem to provide such information. At most, I can see some CPU load in httperf (which in most cases is 100 %, so not too relevant).
Is there some tool that can provide me with memory consumption information ? It doesn't have to be a webserver benchmarking tool, could also be another Linux software that runs in parallel with the benchmarking tool. I just think that manually monitoring the test via the top command is kind of innacurate/ammateurish. Thank you in advance.
htop may be exactly what you're looking for.
Personally, I recently discovered something called byobu - which gives you a handy readout on the bottom (which you can configure by pressing F9) --
And that has become my personal favorite for exactly what you're describing.
Although, you could also look into xdebug -- and use something like xdebug_memory_usage() -- in the php script you're testing to dump info into a log file at key points in your script
I've put up a few PHP cronjobs, too, when I manually start the script through console I want to see debug and stuff, too.
I put in a method like this:
protected $consoleUpdate;
protected function printMemoryUsage() {
if ((time() - $this->consoleUpdate) >= 3) {
$this->consoleUpdate = time();
echo "Memory: ",
round(memory_get_usage(true) / (1024 * 1024)),
" MB",
"\r";
}
}
Call this method as often as you like to print the scripts memory usage.
Notice the final \r in the console, which returns the cursor to the line beginning and overwrites the line. If you don't have any other output, this has the effect of your screen not moving, instead, it gets updated.
Things like top, htop, memstat, iotop, mysqltop. All these tools are excellent to see what is thoroughly cooking your server while you throw siege (and its friend apachebench) at it.
I use vmstat for memory, disk and CPU monitoring. Below are some measurements whilst copying files on a bottom of the heap Linux based Raspberry Pi. I first used vmstat in the 1980’s, monitoring DB activity on early Unix systems. More details in:
http://www.roylongbottom.org.uk/Raspberry%20Pi%20Stress%20Tests.htm
vmstat was either run from a separate terminal or in a combined script file.
pi#raspberrypi /mnt $ time sudo sh -c "cp -r 256k /mnt/new2 && sync"
40 samles at 1 second intervals
vmstat 1 40 > vmstatRes.txt
real 0m38.781s
user 0m0.400s
sys 0m8.400s
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 304684 15208 65952 0 0 0 0 1109 319 39 2 59 0
1 0 0 282788 15208 87308 0 0 10292 0 4018 2994 5 57 9 29
1 1 0 256996 15208 112768 0 0 12380 0 4687 3863 3 53 0 44
2 2 0 231452 15212 138028 0 0 12288 40 4781 4024 5 55 0 40
0 1 0 216576 15216 152476 0 0 7004 10512 5649 3580 5 50 0 46
2 2 0 201688 15216 167288 0 0 7144 17488 5341 3527 2 52 0 46
1 0 0 195064 15216 173808 0 0 3192 9016 5909 3214 2 34 0 64
3 0 0 169520 15216 199152 0 0 12304 0 4704 3914 2 60 0 38
2 3 0 149988 15220 218288 0 0 9252 9892 5003 3614 2 52 0 45
0 2 0 131008 15224 237072 0 0 9112 10324 5086 3568 2 54 0 44
1 0 0 120160 15224 247784 0 0 5232 0 4976 2935 0 34 0 66
0 1 0 110424 15224 257404 0 0 4628 12864 5097 3034 4 36 0 60
1 0 0 86556 15224 281120 0 0 11536 0 4965 3874 3 54 0 43
1 1 0 73784 15224 293816 0 0 6188 11592 5545 3514 2 46 0 52
1 1 0 63252 15232 304132 0 0 4968 10320 4617 2748 2 34 0 64
0 1 0 43148 15232 323960 0 0 9652 7184 5126 3749 2 54 0 43
0 1 0 29336 15232 337560 0 0 6596 10036 4311 2796 2 38 0 59
1 1 0 23944 11696 346276 0 0 7480 0 5465 3455 2 46 0 52
2 1 0 23076 9580 349184 0 0 2860 10524 4521 2323 1 35 0 64
2 1 0 24440 5300 351508 0 0 8864 5188 4586 3215 1 66 0 33
0 1 0 24500 3900 352704 0 0 4896 11448 5974 3308 2 49 0 49
1 1 0 24432 3772 352700 0 0 10424 6208 4851 3682 2 60 0 38
1 1 0 23764 3772 353736 0 0 6568 5184 5970 3526 1 45 0 53
1 1 0 24068 3776 353500 0 0 4900 11388 5449 3142 0 40 0 60
0 1 0 24400 3780 352552 0 0 10068 8848 4821 3531 2 57 0 40
1 1 0 24152 3772 352588 0 0 8292 2784 5207 3588 2 50 0 48
1 1 0 23516 3772 353620 0 0 6800 7816 5475 3475 1 49 0 49
0 1 0 24260 3772 352940 0 0 7004 7424 5042 3284 4 43 0 52
2 1 0 24068 3776 353060 0 0 4624 10292 4798 2801 0 39 0 61
2 0 0 23820 3780 353340 0 0 8844 5508 5251 3609 0 56 0 44
2 1 0 24252 3772 352528 0 0 4552 12000 5053 2841 2 44 0 54
1 1 0 23696 3772 353120 0 0 10880 2176 4908 3694 2 58 0 40
1 0 0 24260 3772 352212 0 0 3748 11104 5208 2904 2 34 0 63
3 2 0 24136 3780 352084 0 0 10148 1628 4637 3568 1 55 0 44
0 1 0 24192 3780 352120 0 0 4016 10260 4719 2613 1 31 0 68
1 1 0 24392 3772 352076 0 0 6804 10972 5386 3473 1 52 0 47
1 1 0 24392 3772 351704 0 0 8568 8788 5101 3502 2 61 0 36
0 1 0 24376 3780 351764 0 0 0 30036 6711 1888 0 36 0 64
0 1 0 24252 3780 351928 0 0 28 2072 5629 1354 0 10 0 90
0 0 0 24768 3780 351968 0 0 40 20 1351 579 9 6 13 72
1 0 0 24768 3780 351968 0 0 0 0 1073 55 1 1 98 0

Find number of occurrence of given value within string?

Consider a string as below .
$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";
I have to find out how many numbers are there within this string. But the number is equal to 1000 and above 1000 which proceed and followed by $ symbol .
Example : $1000 (or) $ 1000 (or) 1000$ (or) 1000 $ and above 1000 only .
Using preg_match_all() and a foreach loop:
$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 50000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";
preg_match_all('/(\$\s?)(?P<before>\d{4,})|(?P<after>\d{4,})(\s?\$)/', $string, $m);
$tmp = array_filter($m["before"]) + array_filter($m["after"]);
$number = array();
foreach($tmp as $n){
if($n >= 1000){
if(isset($number[$n])){
$number[$n]++;
}else{
$number[$n] = 1;
}
}
}
print_r($number);
// Key => number, value => n occurences
I've used \d{4,} to match 4 digit numbers which are 1000 or higher, but say for example there is a number like 0500, this will also be matched. So I used a foreach loop to filter the numbers.
Try this :
$string ="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";
preg_match_all('/\$\s?(?P<pr>\d{4,})|(?P<fl>\d{4,})\s?\$/',$string,$match);
$res = array_merge(array_filter($match['pr']),array_filter($match['fl']));
echo "<pre>";
print_r($res);
Output :
Array
(
[0] => 1000
[1] => 2000
[2] => 5000
[3] => 1020
[4] => 2000
[5] => 3000
)
<?php
$string="Lorem ipsum $ 1000 ,ipsum $2000 sopr $250 gerb $ 150 dfkuer fsdf erwer 1020 $ gsdfasdtwe qw $ 5000 efk kdfgksgdf 2000 $ sdhfgsd fsdf 620 $ sdfjg jsdf3000$";
$pattern = "#([$][\s]*)?([1-9]\d{3})([\s]*[$])?#";
//(?<=$|$\s)
//(?=$|\s$)
preg_match_all($pattern, $string, $out);
print_r($out[2]);
Array
(
[0] => 1000
[1] => 2000
[2] => 1020
[3] => 5000
[4] => 2000
[5] => 3000
)

Categories