title: Split string to parts and load to database
body: <p>I am trying to write a php script which would take a string value, really long one, like:</p>
  
  <pre><code>host.canada.example.com ID: 1402944178480070 Unix hostname: ns1.example.com IP address: 232.99.185.193 MAC address: 02:10:00:2b:33:fb Description: Type: KVM Type code: kvm Status: Virtualmin Status code: virt Status changed: 15/Aug/2014 07:40 OS: CentOS Linux OS code: centos Webmin version: 1.690 Webmin OS type: exampleOS Linux Webmin OS version: 6.5 Virtualmin version: 4.09.gpl Webmin URL: https://host.canada.example.com:10000/ Created: 16/Jun/2014 14:42 Age: 65 days, 3 hours, 36:00 minutes Kernel version: Linux 2.6.32-431.17.1.el6.i686 CPU architecture: i686 Attempt SSH login: Yes SSH login: root SSH password: gxzPksT2 SSH version: 2 SSH port: 22 Webmin same as SSH: No Webmin login: root Webmin password: 5VXrAQPTw Webmin port: 10000 Webmin SSL: Yes Start at boot: Yes Boot device: c Hosted on: canada.example.com KVM process ID: 7026 KVM total disk size: 81.50 GB KVM disk driver: VirtIO KVM high resolution graphics?: No Block spoofed IPs and MACs?: No Memory limit: 2 GB Maximum CPU use: Unlimited Virtual CPUs: 6 Virtual cores: 6 Disk space used on host: 81.50 GB IO class: 0 Allowed IPs: Any Resource accounting period: One month Start of period: 01/Aug/2014 00:00 (1707538 seconds ago) End of period: 20/Aug/2014 18:15 (234 seconds ago) System uptime over period: 19 days, 18 hours, 15:04 minutes (1707304 seconds) Memory used over period: 948.50 GB hours (3666407422164992 byte seconds) Memory average over period: 2147483648 bytes Memory average while up: 2147483648 bytes CPU allocated over period: 47425.11 percent hours (170730400 percent seconds) CPU used over period: 13760.24 percent hours (49536856 percent seconds) CPU used average over period: 29 percent CPU used average while up: 29 percent Disk limit over period: 30686.78 GB hours (118618837351399424 byte seconds) Disk used over period: 30686.78 GB hours (118618837351399424 byte seconds) Disk average over period: 69477279588 bytes CPU load: 0.43 0.37 0.34 Real memory: 1.85 GB Real memory free: 712.57 MB Virtual memory: 1.50 GB Virtual memory free: 891.25 MB Disk space: 78.74 GB Disk space free: 62.39 GB Filesystem: / size 78.74 GB used 12.34 GB free 62.39 GB Filesystem: /dev/shm size 946.59 MB used 0 bytes free 946.59 MB Running services: Apache Webserver, BIND DNS Domain, Postfix Mail Domain, Dovecot IMAP / POP3 Domain, SSH Domain, MySQL Database Domain Down services: 
  </code></pre>
  
  <p>split it into parts and load to a MySQL database. If I output similar string putting it between <code>&lt;pre&gt;</code> and <code>&lt;/pre&gt;</code> tags then I can see it is split to lines like:</p>
  
  <pre><code>anotherhost.canada.example.com
  ID: 1403913554296370
  Unix hostname: den.canada.example.com
  IP address: 232.99.185.195
  MAC address: 00:51:56:04:43:ea
  Description: 
  Type: KVM
  Type code: kvm
  Status: Virtualmin
  Status code: virt
  Status changed: 16/Aug/2014 15:40
  OS: CentOS Linux
  OS code: centos
  Webmin version: 1.690
  Webmin OS type: exampleOS Linux
  Webmin OS version: 6.5
  Virtualmin version: 4.09.gpl
  Webmin URL: https://zabbix.canada.example.com:10000/
  Created: 27/Jun/2014 19:59
  Age: 53 days, 22 hours, 59:25 minutes
  Kernel version: Linux 2.6.32-431.20.3.el6.i686
  CPU architecture: i686
  Attempt SSH login: Yes
  SSH login: root
  SSH password: 1WYKBwa8sEV
  SSH version: 2
  SSH port: 19759
  Webmin same as SSH: Yes
  Webmin port: 10000
  Webmin SSL: Yes
  Start at boot: Yes
  Boot device: c
  Hosted on: canada.example.com
  KVM process ID: 12148
  KVM total disk size: 11.50 GB
  KVM disk driver: VirtIO
  KVM high resolution graphics?: No
  Block spoofed IPs and MACs?: No
  Memory limit: 1.50 GB
  Maximum CPU use: Unlimited
  Virtual CPUs: 1
  Virtual cores: 1
  Disk space used on host: 11.50 GB
  IO class: 0
  Allowed IPs: Any
  Resource accounting period: One month
  Start of period: 01/Aug/2014 00:00 (1709925 seconds ago)
  End of period: 20/Aug/2014 18:55 (221 seconds ago)
  System uptime over period: 19 days, 18 hours, 55:04 minutes (1709704 seconds)
  Memory used over period: 712.38 GB hours (2753671037190144 byte seconds)
  Memory average over period: 1610612736 bytes
  Memory average while up: 1610612736 bytes
  CPU allocated over period: 47491.78 percent hours (170970400 percent seconds)
  CPU used over period: 6806.22 percent hours (24502393 percent seconds)
  CPU used average over period: 14 percent
  CPU used average while up: 14 percent
  Disk limit over period: 5461.55 GB hours (21111477951791104 byte seconds)
  Disk used over period: 5461.55 GB hours (21111477951791104 byte seconds)
  Disk average over period: 12348030976 bytes
  CPU load: 0.08 0.09 0.09
  Real memory: 1.48 GB
  Real memory free: 445.94 MB
  Virtual memory: 1.50 GB
  Virtual memory free: 820.61 MB
  Disk space: 9.84 GB
  Disk space free: 1.63 GB
  Filesystem: / size 9.84 GB used 7.71 GB free 1.63 GB
  Filesystem: /dev/shm size 757.23 MB used 0 bytes free 757.23 MB
  Running services: Apache Webserver, BIND DNS Domain, Postfix Mail Domain, Dovecot IMAP / POP3 Domain, SSH Domain, MySQL Database Domain
  Down services: 
  </code></pre>
  
  <p>but it still is just a string. I guess I could turn this into array, but it is still confusing how do I do it and how to make the php string to correctly put everything into right MySQL database tables.</p>
  
  <p>Edit: I have some progress. With the following piece of code:</p>
  
  <pre><code>$string = '&lt;pre&gt;'.$string.'&lt;/pre';
  $string = split("\n",$string);
  $func = function($value) { return preg_split("/\s{2,}/",$value ); };
  print_r(array_map($func, $string));
  </code></pre>
  
  <p>I've got this array:</p>
  
  <pre><code>Array ( [0] =&gt; Array ( [0] =&gt;
  </code></pre>
  
  <p>host.canada.example.com
          )</p>
  
  <pre><code>[1] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; ID: 1402945178280070
      )
  
  [2] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Unix hostname: ns1.domain.tld
      )
  
  [3] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; IP address: 232.99.185.193
      )
  
  [4] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; MAC address: 02:00:04:2b:33:fb
      )
  
  [5] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Description: 
      )
  
  [6] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Type: KVM
      )
  
  [7] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Type code: kvm
      )
  
  [8] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Status: Virtualmin
      )
  
  [9] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Status code: virt
      )
  
  [10] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Status changed: 15/Aug/2014 07:40
      )
  
  [11] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; OS: CentOS Linux
      )
  
  [12] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; OS code: centos
      )
  
  [13] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin version: 1.690
      )
  
  [14] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin OS type: exampleOS Linux
      )
  
  [15] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin OS version: 6.5
      )
  
  [16] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Virtualmin version: 4.09.gpl
      )
  
  [17] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin URL: https://host.canada.example.com:10000/
      )
  
  [18] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Created: 16/Jun/2014 14:42
      )
  
  [19] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Age: 65 days, 5 hours, 05:55 minutes
      )
  
  [20] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Kernel version: Linux 2.6.32-431.17.1.el6.i686
      )
  
  [21] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU architecture: i686
      )
  
  [22] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Attempt SSH login: Yes
      )
  
  [23] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; SSH login: root
      )
  
  [24] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; SSH password: VXrAQPTw
      )
  
  [25] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; SSH version: 2
      )
  
  [26] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; SSH port: 19759
      )
  
  [27] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin same as SSH: No
      )
  
  [28] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin login: root
      )
  
  [29] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin password: VXrAQPTw
      )
  
  [30] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin port: 10000
      )
  
  [31] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Webmin SSL: Yes
      )
  
  [32] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Start at boot: Yes
      )
  
  [33] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Boot device: c
      )
  
  [34] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Hosted on: canada.example.com
      )
  
  [35] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; KVM process ID: 7026
      )
  
  [36] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; KVM total disk size: 81.50 GB
      )
  
  [37] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; KVM disk driver: VirtIO
      )
  
  [38] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; KVM high resolution graphics?: No
      )
  
  [39] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Block spoofed IPs and MACs?: No
      )
  
  [40] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Memory limit: 2 GB
      )
  
  [41] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Maximum CPU use: Unlimited
      )
  
  [42] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Virtual CPUs: 6
      )
  
  [43] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Virtual cores: 6
      )
  
  [44] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk space used on host: 81.50 GB
      )
  
  [45] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; IO class: 0
      )
  
  [46] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Allowed IPs: Any
      )
  
  [47] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Resource accounting period: One month
      )
  
  [48] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Start of period: 01/Aug/2014 00:00 (1712933 seconds ago)
      )
  
  [49] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; End of period: 20/Aug/2014 19:45 (224 seconds ago)
      )
  
  [50] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; System uptime over period: 19 days, 19 hours, 45:09 minutes (1712709 seconds)
      )
  
  [51] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Memory used over period: 951.50 GB hours (3678014571282432 byte seconds)
      )
  
  [52] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Memory average over period: 2147483648 bytes
      )
  
  [53] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Memory average while up: 2147483648 bytes
      )
  
  [54] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU allocated over period: 47575.25 percent hours (171270900 percent seconds)
      )
  
  [55] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU used over period: 13811.84 percent hours (49722630 percent seconds)
      )
  
  [56] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU used average over period: 29 percent
      )
  
  [57] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU used average while up: 29 percent
      )
  
  [58] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk limit over period: 30809.14 GB hours (119091828677935104 byte seconds)
      )
  
  [59] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk used over period: 30809.14 GB hours (119091828677935104 byte seconds)
      )
  
  [60] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk average over period: 69534187464 bytes
      )
  
  [61] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; CPU load: 0.23 0.25 0.30
      )
  
  [62] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Real memory: 1.85 GB
      )
  
  [63] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Real memory free: 705.05 MB
      )
  
  [64] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Virtual memory: 1.50 GB
      )
  
  [65] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Virtual memory free: 892.21 MB
      )
  
  [66] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk space: 78.74 GB
      )
  
  [67] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Disk space free: 62.39 GB
      )
  
  [68] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Filesystem: / size 78.74 GB used 12.35 GB free 62.39 GB
      )
  
  [69] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Filesystem: /dev/shm size 946.59 MB used 0 bytes free 946.59 MB
      )
  
  [70] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Running services: Apache Webserver, BIND DNS Domain, Postfix Mail Domain, Dovecot IMAP / POP3 Domain, SSH Domain, MySQL Database Domain
      )
  
  [71] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Down services: 
      )
  
  [72] =&gt; Array
      (
          [0] =&gt; 
  </code></pre>
  
  <p>Now, let's guess there is a MySQL database with "id", "unix hostname", "ip address", etc. tables. How can I get value parts like "1402945178280070", "ns1.domain.tld", "232.99.185.193" from the array and to put them into corresponding database tables? </p>
  
  <p>Unfortunately, I can't rely to array item numbers as they constantly change. I mean this following array member:</p>
  
  <pre><code>[2] =&gt; Array
      (
          [0] =&gt; 
          [1] =&gt; Unix hostname: ns1.domain.tld
  </code></pre>
  
  <p>could easily become:</p>
  
  <pre><code>[3] =&gt; Array
  (
      [0] =&gt; 
      [1] =&gt; Unix hostname: ns1.domain.tld
  </code></pre>
  
  <p>next time.</p>