Parsing txt file to rows and columns in PHP -
i new php , have done light web design trying parse text file row , columns , cannot return correct format. here php file
<?php $txt_file = file_get_contents('/var/www/html/config/configfiles/showpeers/output.txt'); $txt_file = str_replace('/\s+/', '_', $txt_file); $rows = explode("\n", $txt_file); //array_shift($rows); foreach($rows $row => $data) { //get row data $row_data = explode('_', $data); $info[$row]['name/username'] = $row_data[0]; $info[$row]['host'] = $row_data[1]; $info[$row]['dyn'] = $row_data[2]; $info[$row]['forceport'] = $row_data[3]; $info[$row]['comedia'] = $row_data[4]; $info[$row]['acl'] = $row_data[4]; $info[$row]['port'] = $row_data[4]; $info[$row]['status'] = $row_data[7]; $info[$row]['description'] = $row_data[8]; //display data echo ' peer: ' . $info[$row]['name/username'] . '<br />'; echo ' host: ' . $info[$row]['host'] . '<br />'; echo ' dyn: ' . $info[$row]['dyn'] . '<br />'; echo ' forceport: ' . $info[$row]['forceport'] . '<br />'; echo ' comedia: ' . $info[$row]['comedia'] . '<br />'; echo ' acl: ' . $info[$row]['acl'] . '<br />'; echo ' port: ' . $info[$row]['port'] . '<br />'; echo ' status: ' . $info[$row]['status'] . '<br />'; echo ' description: ' . $info[$row]['description'] . '<br />'; echo '<br />'; } ?> the txt file
name/username host dyn forcerport comedia acl port status description
1001 (unspecified) d auto (no) no 0 unmonitored
1002 (unspecified) d auto (no) no 0 unmonitored
1003 (unspecified) d auto (no) no 0 unmonitored
1401/1401 192.168.21.94 d auto (no) no 58790 ok (50 ms)
1402 (unspecified) d auto (no) no 0 unmonitored
1403 (unspecified) d auto (no) no 0 unmonitored
1404 (unspecified) d auto (no) no 0 unmonitored
1405 (unspecified) d auto (no) no 0 unmonitored
1406 (unspecified) d yes yes 0 unmonitored
1407 (unspecified) d yes yes 0 unmonitored
1408 (unspecified) d yes yes 0 unmonitored
1409 (unspecified) d auto (no) no 0 unmonitored
1410 (unspecified) d auto (no) no 0 unmonitored
1411 (unspecified) d auto (no) no 0 unmonitored
1412 (unspecified) d auto (no) no 0 unmonitored
1413 (unspecified) d auto (no) no 0 unmonitored
1414 (unspecified) d auto (no) no 0 unmonitored
1415 (unspecified) d auto (no) no 0 unmonitored
1420 (unspecified) d auto (no) no 0 unmonitored
2801 (unspecified) d auto (no) no 0 unmonitored
2803 (unspecified) d auto (no) no 0 unmonitored
2805 (unspecified) d auto (no) no 0 unmonitored
mcd_demo 192.168.22.37 yes yes 5060 ok (19 ms)
ncp 192.168.21.74 yes yes 5060 unreachable
stg01 192.168.27.50 yes yes 5060 unreachable
switchvox/switchvox (unspecified) d yes yes 0 unknown
26 sip peers [monitored: 2 online, 3 offline unmonitored: 0 online, 21 offline]
my output looks image below. trying rows populate off spaces not working properly.

thank you
thank guys, able figure out. below new php
<?php $txt_file = file_get_contents('/var/www/html/config/configfiles/showpeers/output.txt'); $txt_file = preg_replace('/\h+/', ' ', $txt_file); $rows = explode("\n", $txt_file); array_shift($rows); array_pop($rows); array_pop($rows); foreach($rows $row => $data) { //get row data $row_data = explode(' ', $data); $info[$row]['name/username'] = $row_data[0]; $info[$row]['host'] = $row_data[1]; $info[$row]['dyn'] = $row_data[2]; $info[$row]['forceport'] = $row_data[3]; $info[$row]['comedia'] = $row_data[4]; $info[$row]['acl'] = $row_data[4]; $info[$row]['port'] = $row_data[4]; $info[$row]['status'] = $row_data[7]; $info[$row]['description'] = $row_data[8]; //display data echo ' peer: ' . $info[$row]['name/username'] . '<br />'; echo ' host: ' . $info[$row]['host'] . '<br />'; //echo ' dyn: ' . $info[$row]['dyn'] . '<br />'; //echo ' forceport: ' . $info[$row]['forceport'] . '<br />'; //echo ' comedia: ' . $info[$row]['comedia'] . '<br />'; //echo ' acl: ' . $info[$row]['acl'] . '<br />'; //echo ' port: ' . $info[$row]['port'] . '<br />'; echo ' status: ' . $info[$row]['status'] . '<br />'; //echo ' description: ' . $info[$row]['description'] . '<br />'; echo '<br />'; } ?>
Comments
Post a Comment