joink Posted April 13, 2010 Share Posted April 13, 2010 Hi I am trying to write a php script for creating a graphical network map to determine outages for a setup I have... It's been a while that Ive worked with PHP and could use some help, Im sure if I search long enough I'll figure out everything I need. Maybe you can help save me some time? What I have now is a text based network map, which follows. There is some formatting.. Indents and bullets next to each line. Last Updated 4/13/2010, 12:15:00 PM br-ths.inds.th_ind_s, 30/30 beats, 4 ms/4 ms/4 ms, UP br-thind.ths.south.th, 30/30 beats, 5 ms/5 ms/5 ms, UP br-core.prov.core.th, 30/30 beats, 4 ms/4 ms/4 ms, UP rtr.core.th, 30/30 beats, 54 ms/15 ms/15 ms, UP sw1.core.th, 30/30 beats, 4 ms/1 ms/1 ms, UP br-frm.core.th, 30/30 beats, 4 ms/3 ms/3 ms, UP br-core.main.farmersburg, 30/30 beats, 4 ms/4 ms/4 ms, UP br-datalabel_1313.core.th, 30/30 beats, 1 ms/0 ms/0 ms, UP br-core.1313.th, 30/30 beats, 3 ms/2 ms/2 ms, UP sw.1313.th, 30/30 beats, 4 ms/4 ms/4 ms, UP br-core.datalabel.core.th, 30/30 beats, 1 ms/1 ms/1 ms, UP datalabel-router.core.th, 30/30 beats, 3 ms/2 ms/4 ms, UP br-13thST.core.th, 30/30 beats, 1 ms/1 ms/1 ms, UP br-core.13thST.th, 30/30 beats, 2 ms/2 ms/2 ms, UP rtr.13thST.th, 30/30 beats, 2 ms/3 ms/5 ms, UP sw.13thST.th, 30/30 beats, 10 ms/7 ms/7 ms, UP Can I call this file and create a couple variables based on each line? I think truncating is what I need... where I would create a variable up to the first comma and another variable following the last comma. So for first line (after posted update time/date) $br-ths.inds.th_ind_s = 'UP' or something like $br-ths.inds.th_ind_s = $status In the end I will have a network map with colors to represent if each device is up or down (red or green) however the indented (child) devices below the parent devices, if they are down and the parent is up I will have display yellow (if less than half the child devices are down) if more than half is down, the color will be orange. Hope this makes sense, and hope you can help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198406-truncate/ Share on other sites More sharing options...
teamatomic Posted April 13, 2010 Share Posted April 13, 2010 list($router,$metric,$ping,$status)=explode(",",$line); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198406-truncate/#findComment-1041109 Share on other sites More sharing options...
joink Posted April 13, 2010 Author Share Posted April 13, 2010 Thanks for that! What I have now: <?php $file_handle = fopen("networkmap.asp", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); // Strip HTML formatting $content = strip_tags($line_of_text); $strip_content = substr($content, 34); list($router,$metric,$ping,$status)=explode(",",$strip_content); } fclose($file_handle); echo $router; echo $metric; echo $ping; echo $status; ?> Is there an easier way than a whole bunch of if/and statements with this setup? Can I identify each exploded result as a variable automatically? if ($router=='br-ths.inds.th_ind_s' && $status='UP') { $stat_img = 'green.png'; } else { and so forth... Is there an easier way to identify each exploded variable? I may have as many as 100 or more to monitor... And lastly how do I associate each variable to each line. Where $status for one router is not the same $status for another line item/router? Quote Link to comment https://forums.phpfreaks.com/topic/198406-truncate/#findComment-1041291 Share on other sites More sharing options...
jcbones Posted April 13, 2010 Share Posted April 13, 2010 <?php $file = file("networkmap.asp"); foreach(file('networkmap.asp') as $value) { // Strip HTML formatting $content = strip_tags($value); $strip_content = substr($content, 34); list($router,$metric,$ping,$status)=explode(",",$strip_content); echo $router; echo $metric; echo $ping; echo ($status == 'UP') ? '<img src="green.png" alt="up" />' : '<img src="red.png" alt="down" />'; echo '<hr />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/198406-truncate/#findComment-1041295 Share on other sites More sharing options...
teamatomic Posted April 14, 2010 Share Posted April 14, 2010 $file = file("test.txt",FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES); array_shift($file); foreach($file as $value) { $content = strip_tags($value); // $content=trim($content); list($router,$metric,$ping,$status)=explode(",",$content); $status=trim($status); $img = ($status == 'UP') ? '<img src="green.png" alt="up" />' : '<img src="red.png" alt="down" />'; echo "$router $img<hr />"; } HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/198406-truncate/#findComment-1041370 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.