Boxerman Posted November 18, 2015 Share Posted November 18, 2015 (edited) Hey guys, I have written an output like the following: 412,Processor 1 CPU 1 VR Temp,Temperature,N/A,N/A,'OK' as you can see after the 5th comma an output is 'OK' How would one go about chasing the 5th comma and echoing out the OK part? Hopefully this will make you laugh and be super simple! Thanks ahead of time all! B-Man Edited November 18, 2015 by Boxerman Quote Link to comment Share on other sites More sharing options...
boompa Posted November 18, 2015 Share Posted November 18, 2015 Use explode. 1 Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 18, 2015 Author Share Posted November 18, 2015 Thanks!! So, i managed to get this far.. echo "<br>"; echo '<pre>'; print_r(explode(',', $stream)); echo '</pre>'; echo '<br>'; This is the output: Array ( [0] => 1 [1] => Front Panel Board Ambient Temp [2] => Temperature [3] => 16.00 [4] => C [5] => 'OK' 2 [6] => System Board Planar 3.3V [7] => Voltage [8] => 3.34 [9] => V [10] => 'OK' 3 [11] => System Board Planar 5V [12] => Voltage [13] => 5.06 [14] => V [15] => 'OK' 4 [16] => System Board Planar 12V [17] => Voltage [18] => 12.10 [19] => V [20] => 'OK' As you can see this is sort of doing what it needed... The new line numbers for 1 is different to 2,3 and 4.. Unformatted output is: 1,Front Panel Board Ambient Temp,Temperature,16.00,C,'OK' 2,System Board Planar 3.3V,Voltage,3.34,V,'OK' 3,System Board Planar 5V,Voltage,5.06,V,'OK' 4,System Board Planar 12V,Voltage,12.10,V,'OK' SO my question is, how do i get it to spilt each line as a new one? like the unformatted one, the aim is to check the end "OK" and if it is, ignore it but if it is anything that is not "OK" then display on the page.. I'm confusing me a little, any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2015 Share Posted November 18, 2015 OK, so what you told us in the first post was just a small part of the whole story. Is that "unformatted output" held in a text file? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted November 18, 2015 Share Posted November 18, 2015 (edited) the answer is still to use explode (unless you are getting this from a file, in which case file() would be the best first step.). you explode first on the line-ending-character (varies depending on where you are getting the data from, can be \r, \n, \r\n, or \n\r), to give an array of individual lines. you then explode each line on the comma character. you would want to trim() the final data before you try to use it in case there's some white-space characters as part of the data. Edited November 18, 2015 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2015 Share Posted November 18, 2015 if it's in a file, fgetcsv() would be my weapon of choice. Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 18, 2015 Author Share Posted November 18, 2015 Hi All, Thanks for the advise! This is ran via ssh2, and stored into the $stream variable. The end goal would be to scan the output for anything that does not have the "OK" in. If if doesn't have the "OK" then I want it to echo out that entire line. What would you recommend? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2015 Share Posted November 18, 2015 does this do it? $data = explode("\n", $stream); foreach ($data as $line) { $arr = str_getcsv($line,',', "'"); if ($arr[5] != 'OK') { echo $line.'<br>'; } } 1 Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 18, 2015 Author Share Posted November 18, 2015 Wow thats exactly what i wanted! Thank you so much, How would i go about counting total in the array and then comparing to total OK and total NOT OK? for example: Testing 400 sensors, 380 OK and 20 NOT OK. 20 not ok entires listed. Super sorry, and thank you, for taking the time to help a man in need! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2015 Share Posted November 18, 2015 like this $data = explode("\n", $stream); $fail=0; foreach ($data as $line) { $arr = str_getcsv($line,',', "'"); if ($arr[5] != 'OK') { echo $line.'<br>'; ++$fail; } } $tot = count($data); printf("<hr>%d sensors tested<br>%d OK<br>%d failed", $tot, $tot-$fail, $fail); 1 Quote Link to comment Share on other sites More sharing options...
Boxerman Posted November 18, 2015 Author Share Posted November 18, 2015 Ah i see how you're doing it, how would you move that above the output? trying to move $line is not working (i presume its outside the IF so the variable doesn't work)? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 18, 2015 Share Posted November 18, 2015 It counts them while it is processing and outputting them. If you want the list afterwards then it needs to be stored then printed after the counts are printed. $data = explode("\n", $stream); $fail=0; $failed = ''; foreach ($data as $line) { $arr = str_getcsv($line,',', "'"); if ($arr[5] != 'OK') { $failed .= $line.'<br>'; // store failures ++$fail; } } $tot = count($data); printf("%d sensors tested<br>%d OK<br>%d failed (listed below)<hr>%s", $tot, $tot-$fail, $fail, $failed); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.