I need to build a script that will query a remote storage server with multiple drives for their free space.
I simply login with SSH, run df and throw the output into a variable. Which looks like this:
Array
(
[0] => Filesystem 1K-blocks Used Available Use% Mounted on
[1] => /dev/sda1 59392320 14949240 41426064 27% /
[2] => /dev/sdd1 140592240 114503840 18946708 86% /home/overflow
[3] => /dev/sdf1 287826944 273844324 0 100% /home/node2
[4] => /dev/sde1 287826944 253278964 19927228 93% /home/node3
[5] => /dev/sdg1 287826944 4771768 268434424 2% /home/node4
[6] => /dev/sdh1 287826944 4329780 268876412 2% /home/node5
[7] => /dev/sdi1 488302976 439077756 24420864 95% /home/node1
[8] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode6
[9] => /dev/sdh1 287826944 4329780 268876412 2% /home/mnode7
[10] => tmpfs 3145728 0 3145728 0% /tmp
)
I need to extract how much free space each "node" or "mnode" has. So perhaps a nice array that has pairs such as this:
mnode1 => 24000000
node1 => 24000000
node2 => 0
node3 => 20000000
Use explode(' ', preg_replace('/ +/', ' ', $line)) to get an array with all values split up.
Related
I want to store sub category name and products related to that particular subcategory in array.
Below is my code,
f ($sub_category->num_rows > 1) {
while ($row = mysqli_fetch_array($sub_category)) {
$subcategory_id = $row['sub_cat_id'];
$subcategory_name['sub_category_name'][] = $row['sub_cat_name'];
$sql_subrec = "SELECT es_product_description.name AS prod_name FROM es_product_to_category LEFT JOIN es_product_description ON es_product_description.product_id=es_product_to_category.product_id LEFT JOIN es_product ON es_product_description.product_id = es_product.product_id WHERE es_product_to_category.category_id = $subcategory_id AND es_product.status=1";
//echo "<pre>";print_r($sql_subrec);
$sub_product = $conn->query($sql_subrec);
while ($prow = mysqli_fetch_assoc($sub_product)) {
$subcategory_name['sub_category_name']['products_name'][] = $prow['prod_name'];
}
}
//$subcategory_name[] = array('product_data' => $subcategory_name,);
//echo "<pre>";print_r($subcategory_name);
echo "<pre>";print_r($subcategory_name);
}
Result is not coming as expected.
I want to get products inside related subcategory array but it is coming everything in first array.
Array
(
[sub_category_name] => Array
(
[0] => Weighing Scale
[products_name] => Array
(
[0] => BS-250 Baby Scale
[1] => DS-415N Bench Scale
[2] => DS-415N Platform Scale
[3] => DS-65 Counter Scale
[4] => DS-852 Weighing Scale
[5] => DS-252 Weighing Scale
[6] => DS-215N Platform Scale
[7] => DS-75 Counter Scale
[8] => DS-415 Smile
[9] => DS-215SS Platform Scale
[10] => DS-450 Table Top Weighing Scale
[11] => DS-450SS Bench Scale
[12] => DS-451HP Weighing Scale
[13] => DS-215 Hanging Scale
[14] => DS-215N Trolley Scale
[15] => DS-673 Weighing Scale
[16] => DS-315 Series Crane Scale
[17] => DS-515 Weighing Scale
[18] => DS-450CW Check Weigher Scale
[19] => DC-810 Counting, Barcode Printing Scale
[20] => DC-85 Counting Scale
[21] => DS-252C Counting Scale
[22] => DC-810 Counting, Barcode Printing Scale
[23] => SI-810 Label Printing Scale
[24] => SI-810PR Receipt Printing
[25] => DS-252PR Receipt Printing Scale
[26] => Barcode Label Printer Scales
[27] => Receipt Printer Scales
[28] => SI-810PR Barcode Label Printer Scale
[29] => SM - 100EV+ Barcode Label Printer Scale
[30] => SI-810PRSS Receipt Printer Scale
[31] => SI-810PR Receipt Printer Scale
[32] => DS-252PR Receipt Printer Scale
[33] => DC-810 Counting, Barcode Printing Scale
[34] => SI-810 Label Printing Scale
[35] => DS-451TW Tank/Vessel Weighing
[36] => DS-451 Milk Weighing
[37] => SI-810 System Scale Bench Type
[38] => SI-810 System Scale Platform Type
[39] => HT-HTR Series Analytical Balance
[40] => MX-50 Moisture Analyzer
[41] => AJ Series Precision Balance
[42] => PG/FB Precision Balance
[43] => DS-852G Precision Balance
[44] => LF-225DR Semi-micro balance
)
[1] => Industrial Counting Scale
[2] => Retail Printer Scale
[3] => Weighing System & Solutions
[4] => Weighing Balance
)
)
As far as I understand from you limited explanation you might just need to do something like this in your second while:
$subcategory_name['sub_category_name'][$row['sub_cat_name']]['products_name'][] = $prow['prod_name'];
But this is just a guess... please post the expected output and the actual one.
Try this. 'Sub Category Name' & 'Product Name' comes under single 'Sub Category ID' keyindex
if ($sub_category->num_rows > 1){
while ($row = mysqli_fetch_array($sub_category)){
$subcategory_id = $row['sub_cat_id'];
$subcategory_name[$subcategory_id]['sub_category_name'][] = $row['sub_cat_name'];
$sql_subrec = "SELECT es_product_description.name AS prod_name FROM es_product_to_category LEFT JOIN es_product_description ON es_product_description.product_id=es_product_to_category.product_id LEFT JOIN es_product ON es_product_description.product_id = es_product.product_id WHERE es_product_to_category.category_id = $subcategory_id AND es_product.status=1";
//echo "<pre>";print_r($sql_subrec);
$sub_product = $conn->query($sql_subrec);
while ($prow = mysqli_fetch_assoc($sub_product)){
$subcategory_name[$subcategory_id]['products_name'][] = $prow['prod_name'];
}
}
echo "<pre>";print_r($subcategory_name);
}
I am writing a reporting app that needs to consume logs which have been stored in the DB as base 64 encoded strings. I am able to decode them no problem, however, I am having some trouble getting them to be fed into str_getcsv() properly.
Below is the data I am working with, the code and the outputs. It seems to me that once decoded the files are not recognizable as tab-delimited. However, if I decode it with this URL and and save as a text file, I can open it properly in excel.
https://www.base64decode.org/
In PHP however, it seems to be an issue with recognizing some of the tabs and the line breaks seem to completely go away. I think it has to do with the encoding, the DB table and column are both UTF-8. They are being recognized as ASCII - which is a subset of UTF-8, but I am not sure if they need to be explicitly UTF-8 for it to work (the site that works uses UTF-8).
The code: very simple (though at this point I may be going overboard with the encoding)
// get the stored result (laravel eloquent)
$media_result = MediaResult::where("video_id", "=", $media_benchmark->id)->firstOrFail();
# decode the access_log stored as b64 string
$tab_file = base64_decode(mb_convert_encoding($media_result->access_log, "UTF-8"));
$encoding = mb_detect_encoding($tab_file); // I was using iconv() so I grabbed this - it is always ASCII
$new_file = mb_convert_encoding($tab_file,'UTF-8');
$encoding_new = mb_detect_encoding($new_file);
#if I were to echo both encoding variables, it would be ASCII - no matter what I do.
# convert the supposed tab-delimited file into an array
$full_stats = str_getcsv($new_file, 0, "\t");
Here is a sample base64 encoded log:
VVJJCXNlcnZlckFkZHJlc3MJbnV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ubGV2ZWwzLm5ldC92ZXZvL2NoMS8wNi9wcm9nX2luZGV4Lm0zdTgJOC4yNTMuMzIuMTI2CTgJMAkzNS43NDAxNjM2MjIJMTIzNDgxOTcyCTQzCTIwMTYtMDUtMTAgMTk6MjE6MzQgKzAwMDAJN0ExMjkwREMtMTYzMC00OUZDLUFBNjQtQ0M3NkMxODFDNzJBCTU4LjAyODk5NDM1OAlMSVZFCTAJMjQxLjkyNjk3NTk2NTQ5OTkJMAkwCTQzCTI1OAkwCS0xCTQ2ODg1OTAzLjAzNTk4OTkzCTEwODA3NDU3LjM4MjQwNjY3CS0xCTQwMDAwMDAJMzE3ODIzNjAuNjE0NTI4NjM=
Here is the same string decoded:
URI serverAddress numberOfServerAddressChanges mediaRequestsWWAN transferDuration numberOfBytesTransferred numberOfMediaRequests playbackStartDate playbackSessionID playbackStartOffset playbackType startupTime durationWatched numberOfDroppedVideoFrames numberOfStalls numberOfSegmentsDownloaded segmentsDownloadedDuration downloadOverdue observedBitrateStandardDeviation observedMaxBitrate observedMinBitrate switchBitrate indicatedBitrate observedBitrate http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/01/prog_index.m3u8 8.254.23.254 0 0 0.678280709 1099612 3 2016-05-10 19:21:18 +0000 7A1290DC-1630-49FC-AA64-CC76C181C72A 42 LIVE 0.2532979846000671 16.08326596021652 0 0 3 18 0 -1 25572901.38330787 18207987.3285259 10115548.78381825 492000 21025955.05388924 http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/06/prog_index.m3u8 8.253.32.126 8 0 35.740163622 123481972 43 2016-05-10 19:21:34 +0000 7A1290DC-1630-49FC-AA64-CC76C181C72A 58.028994358 LIVE 0 241.9269759654999 0 0 43 258 0 -1 46885903.03598993 10807457.38240667 -1 4000000 31782360.61452863
Finally, here is the resulting array:
Array ( [0] => URI serverAddress numberOfServerAddressChanges mediaRequestsWWAN transferDuration numberOfBytesTransferred numberOfMediaRequests playbackStartDate playbackSessionID playbackStartOffset playbackType startupTime durationWatched numberOfDroppedVideoFrames numberOfStalls numberOfSegmentsDownloaded segmentsDownloadedDuration downloadOverdue observedBitrateStandardDeviation observedMaxBitrate observedMinBitrate switchBitrate indicatedBitrate observedBitrate http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/ [1] => 1/prog_index.m3u8 8.254.23.254 [2] => 0 [3] => .67828 [4] => 7 [5] => 9 1 [6] => 99612 3 2 [7] => 16- [8] => 5-1 [9] => 19:21:18 + [10] => [11] => [12] => [13] => 7A1290DC-1630-49FC-AA64-CC76C181C72A42 LIVE [14] => .2532979846 [15] => [16] => [17] => 671 16. [18] => 8326596 [19] => 21652 [20] => 03 18 [21] => -1255729 [22] => 1.3833 [23] => 787 182 [24] => 7987.3285259 1 [25] => 115548.78381825 492 [26] => [27] => [28] => 21025955.05388924 http://vevoplaylist-live.hls.adaptive.level3.net/vevo/ch1/06/prog_index.m3u88.253.32.126 8 [29] => 35.740163622123481972 43 2 [30] => 16- [31] => 5-1 [32] => 19:21:34 + [33] => [34] => [35] => [36] => 7A1290DC-1630-49FC-AA64-CC76C181C72A58. [37] => 28994358 LIVE [38] => 241.9269759654999 [39] => 043 258 [40] => -1468859 [41] => 3. [42] => 3598993 1 [43] => 8 [44] => 7457.3824 [45] => 667 -1 4 [46] => [47] => [48] => [49] => [50] => [51] => 31782360.61452863 )
Keep in mind that str_getcsv()
parses only one line of a csv file
expects the delimiter "\t" to be the second parameter, not the third
You probably want something like:
$full_stats = [];
foreach(explode("\n", $decoded) as $line) {
$full_stats[] = str_getcsv($line, "\t");
}
var_dump($full_stats);
This will output an array containing 3 arrays (aka rows) containing 24 items (aka columns) each.
See http://sandbox.onlinephpfunctions.com/code/1ccf5115df6f8c342ff7c7e451f3ea26e081197e for working example and generated output.
Regarding the import of data that contains line breaks you should switch to fget_csv() which handles line breaks correctly:
$csv = <<< eot
"first","my data
with line breaks"
"second", "simple data"
eot;
// We need to "convert" the string to a file handle
$fp = fopen('data://text/plain,' . $csv,'r');
while ($data = fgetcsv($fp)) {
var_dump($data);
}
i have found the solution mysqlf using:
foreach ($output as $value) {
if (strpos($value, "]:") > -1) {
$tal = substr($value, strpos($value, "]:") +3) . "<br>";
echo $tal;
}
}
this returns:
-210
-212
Thanks in advance.
I want to preg so i only get the line: [10] => [147]: -210
or both [10] => [147]: -210 and [21] => [148]: -212
how can i preg [147]: or is there a better way to get the specific information?
my array $output contains:
Array
(
[0] => modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
[1] => Copyright (c) 2002-2013 proconX Pty Ltd
[2] => Visit http://www.modbusdriver.com for Modbus libraries and tools.
[3] =>
[4] => Protocol configuration: MODBUS/TCP
[5] => Slave configuration...: address = 1, start reference = 147, count = 1
[6] => Communication.........: 10.234.6.11, port 502, t/o 1.00 s, poll rate 1000 ms
[7] => Data type.............: 16-bit register, output (holding) register table
[8] =>
[9] => -- Polling slave...
[10] => [147]: -210
[11] => modpoll 3.4 - FieldTalk(tm) Modbus(R) Master Simulator
[12] => Copyright (c) 2002-2013 proconX Pty Ltd
[13] => Visit http://www.modbusdriver.com for Modbus libraries and tools.
[14] =>
[15] => Protocol configuration: MODBUS/TCP
[16] => Slave configuration...: address = 1, start reference = 148, count = 1
[17] => Communication.........: 10.234.6.11, port 502, t/o 1.00 s, poll rate 1000 ms
[18] => Data type.............: 16-bit register, output (holding) register table
[19] =>
[20] => -- Polling slave...
[21] => [148]: -212
)
$matches = preg_grep ('/^[147] (\w+)/i', $output);
print_r ($matches);
//only returns Array()
You need to escape the opening square bracket because [147] is seen as a character class that contains 1, 4 and 7
You can do this with:
$result=preg_grep('~^\[14(?:7|8)]:~',$rgData);
print_r($result);
You can find all that you want to know about escaping (or not) square brackets here
I have simple csv file which is tab delimited which i have to use as it is because it is coming from somewhere and i hvae to read it and insert it into my db i have used a simple php code to read it
if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){
/*Skip the first row*/
fgetcsv($handle, 1000,chr(9));
while(($data = fgetcsv($handle,1000,chr(9)))!==FALSE){
print_r($data[0]);
}
}
When print_r the data it shows like
Array ( [0] => 01SATAPC [1] => 40ATAPC [2] => [3] => 21P [4] => SERIAL ATA POWER CABLE [5] => 0.00 [6] => 2.00 [7] => 0 [8] => Power Supplies [9] => SERIAL ATA POWER CABLE [10] =>
4 TO 15 PIN 160MM
[11] => [12] => [13] => [14] => MELBHO [15] => 0.000 [16] => [17] => Order to Order [18] => 4 [19] => 2013-01-18 )
Which is the desired result but when i go to access the particular column value using the $data['index'] e.g. $data[8] or $data[1] it weirdly giving me garbage values says for some iterations it give me right values but after 10-15 rows its starting giving me the some numbers and other column values..... i don't know whats is going on with this as far as i know it should be formatting issue i have tried open the file in excel and its coming fine....
#ravisoni are you sure that the second parameter to fgetcsv of 1000 is longer than the longest line in your file? Try setting it to 0 as the docs say [php.net/fgetcsv] and see if that makes a difference.
if(($handle = fopen("var/import/MMT29DEC.csv","r"))!==FALSE){
/*Skip the first row*/
fgetcsv($handle, 0,chr(9));
while(($data = fgetcsv($handle,0,chr(9)))!==FALSE){
print_r($data[0]);
}
}
Look at IBM's Unicode for the working PHP programmer, especially listings 3 and 4.
On Ubuntu Lucid I get the same output from the code as IBM does, viz:
Здравсствуйте
Array
(
[1] => 65279
[2] => 1047
[3] => 1076
[4] => 1088
[5] => 1072
[6] => 1074
[7] => 1089
[8] => 1089
[9] => 1090
[10] => 1074
[11] => 1091
[12] => 1081
[13] => 1090
[14] => 1077
)
Здравсствуйте
However, on Windows I get a completely different response.
ðùð┤ÐÇð░ð▓ÐüÐüÐéð▓Ðâð╣ÐéðÁ
Array
(
[1] => -131072
[2] => 386138112
[3] => 872677376
[4] => 1074003968
[5] => 805568512
[6] => 839122944
[7] => 1090781184
[8] => 1090781184
[9] => 1107558400
[10] => 839122944
[11] => 1124335616
[12] => 956563456
[13] => 1107558400
[14] => 889454592
)
ðùð┤ÐÇð░ð▓ÐüÐüÐéð▓Ðâð╣ÐéðÁ
Aside from the fact that the Russian characters (which are in UTF-32) don't render in a CMD.EXE shell (because they're in UTF-32 not Windows' own UTF-16), why do the character values differ so significantly?
function utf8_to_unicode_code($utf8_string)
{
$expanded = iconv("UTF-8", "UTF-32", $utf8_string);
return unpack("L*", $expanded);
}
This does two things wrong:
It uses “UTF-32”, which will drop an unwanted BOM at the start of the string, which is why you get 65279 (0xFEFF BOM). You don't want stray BOMs hanging around the place causing trouble.
It uses machine-specific byte endianness (capital L) which iconv may well not agree with. To be honest I wouldn't have expected it to clash on a Windows box (as i386 is little-endian regardless of OS), but clearly it has, as the values you've got are all what would result from a reversed byte order.
Better to state both byte orderings explicitly, and avoid the BOM. Use UCS-4LE as the encoding, and unpack with V*. The same goes for unicode_code_to_utf8.
Also ignore listing 6. The ellipsis character—like the fi-ligature and others—is a ‘compatibility character’ which we wouldn't use in the modern Unicode-and-OpenType world. It's up to the font to provide contextual alternatives for fi or ... if it wants to, instead of requiring us to mangle the text.