Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As you are parsing data that should match a particular format, it should be regular in nature, allowing you to use regular expression to strictly match the input you want (and throw away that you don't):</p> <pre><code>$rows = explode("\n", $input); // @link http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address $regIp = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"; $regHost = "(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])"; $hosts = array(); foreach ($rows as $row) { $matches = null; if (preg_match("/(?&lt;host&gt;$regHost),(?&lt;ip&gt;$regIp),(?&lt;netmask&gt;$regIp),(?&lt;gateway&gt;$regIp),(?&lt;mac&gt;.*)/", $row, $matches)) { $host = $matches['host']; $hosts[$host]['name'] = $matches['host']; $hosts[$host]['mac'] = $matches['mac']; $hosts[$host]['eth'][] = array( 'ip' =&gt; $matches['ip'], 'netmask' =&gt; $matches['netmask'], 'gateway' =&gt; $matches['gateway'], ); } else if (preg_match("/,(?&lt;ip&gt;$regIp),(?&lt;netmask&gt;$regIp),(?&lt;gateway&gt;$regIp),/", $row, $matches)) { $hosts[$host]['eth'][] = array( 'ip' =&gt; $matches['ip'], 'netmask' =&gt; $matches['netmask'], 'gateway' =&gt; $matches['gateway'], ); } else if (preg_match("/,(?&lt;name&gt;.+),(?&lt;size&gt;\d+),,,/", $row, $matches)) { $hosts[$host]['partition'][] = array( 'name' =&gt; $matches['name'], 'size' =&gt; $matches['size'], ); } else if (preg_match("/,,,,,/", $row)) { // we already partition output array with value of `$host` variable. echo "Found terminating row\n"; } else { echo "Unrecognised data on row: $row\n"; } } var_export($hosts); </code></pre> <p>Since we are dealing with a CSV file, one could also use <code>$fields = str_getcsv($row)</code> inside the loop to get an array containing each field. One would then need to count how many fields the array had, how many where empty and validate what each field contained.</p> <p>Using a regular expression directly on the string representation of each CSV row allows us to strictly match all of the above in a single expression. The <code>(?&lt;var&gt;...)</code> parts of each regular expression are named matches. Using the input provided by OP, the above script outputs:</p> <pre><code>Found terminating row Found terminating row array ( 'host1' =&gt; array ( 'name' =&gt; 'host1', 'mac' =&gt; '00:50:56:ac:69:cb', 'eth' =&gt; array ( 0 =&gt; array ( 'ip' =&gt; '10.153.196.248', 'netmask' =&gt; '255.255.255.0', 'gateway' =&gt; '10.153.196.1', ), 1 =&gt; array ( 'ip' =&gt; '10.153.157.113', 'netmask' =&gt; '255.255.255.128', 'gateway' =&gt; '10.153.157.1', ), 2 =&gt; array ( 'ip' =&gt; '10.153.157.241', 'netmask' =&gt; '255.255.255.128', 'gateway' =&gt; '10.153.157.129', ), ), 'partition' =&gt; array ( 0 =&gt; array ( 'name' =&gt; '/home', 'size' =&gt; '10', ), 1 =&gt; array ( 'name' =&gt; 'swap', 'size' =&gt; '10', ), 2 =&gt; array ( 'name' =&gt; '/opt', 'size' =&gt; '60', ), 3 =&gt; array ( 'name' =&gt; '/data', 'size' =&gt; '30', ), ), ), 'host2' =&gt; array ( 'name' =&gt; 'host2', 'mac' =&gt; '00:50:56:ac:69:ce', 'eth' =&gt; array ( 0 =&gt; array ( 'ip' =&gt; '10.153.155.124', 'netmask' =&gt; '255.255.255.128', 'gateway' =&gt; '10.153.155.1', ), 1 =&gt; array ( 'ip' =&gt; '10.153.157.114', 'netmask' =&gt; '255.255.255.128', 'gateway' =&gt; '10.153.157.1', ), 2 =&gt; array ( 'ip' =&gt; '10.153.157.242', 'netmask' =&gt; '255.255.255.128', 'gateway' =&gt; '10.153.157.129', ), ), 'partition' =&gt; array ( 0 =&gt; array ( 'name' =&gt; '/home', 'size' =&gt; '10', ), 1 =&gt; array ( 'name' =&gt; 'swap', 'size' =&gt; '10', ), 2 =&gt; array ( 'name' =&gt; '/opt', 'size' =&gt; '60', ), 3 =&gt; array ( 'name' =&gt; '/data', 'size' =&gt; '30', ), ), ), ) </code></pre> <p>As we now have an array of properly structured data, it should be trivial to output the data in any format required (ie. HTML table).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload