arcronis arcronis - 3 months ago 25
PHP Question

Split PHP String to Array

I have a string from an API and need to get this into an array to work with it but I don't find any solution to this. I hope someone can help.

the string looks like this:


 domain: xxx.de native_domain: xxx.de user: master status: active
transfer_lock: yes auto_renew: yes delegation: yes ownerc: ASDASD1
adminc: ASDASD1 techc: ASDASD1 billc: FLOSCH2 created: 2015-08-25
10:31:08 modified: 2016-08-20 16:20:15 expire: 2017-08-25 10:31:13
chreseller_auth: dom-cdmv-zs0f-mpwg auth_info: dns::1/hostname:
dns1.xxx.com dns::1/hostip: dns::2/hostname: dns2.xxx.com
dns::2/hostip: dns::3/hostname: dns::3/hostip: dns::4/hostname:
dns::4/hostip: dns::5/hostname: dns::5/hostip: comment: inserted:
2016-01-29 23:46:29



full string like this

domain: xxx.de native_domain: xxx.de user: master status: active
transfer_lock: yes auto_renew: yes delegation: yes ownerc: ASDASD1
adminc: ASDASD1 techc: ASDASD1 billc: FLOSCH2 created: 2015-08-25
10:31:08 modified: 2016-08-20 16:20:15 expire: 2017-08-25 10:31:13
chreseller_auth: dom-cdmv-zs0f-mpwg auth_info: xxxxx_authinfo
dns::1/hostname:
dns1.xxx.com dns::1/hostip: 1.1.1.1 dns::2/hostname: dns2.xxx.com
dns::2/hostip: 1.1.1.1 dns::3/hostname: dns3.xxx.com
dns::3/hostip: 1.1.1.1 dns::4/hostname: dns4.xxx.com
dns::4/hostip: 1.1.1.1 dns::5/hostname: dns5.xxx.com dns::5/hostip:
1.1.1.1 comment: comments inserted:
2016-01-29 23:46:29


Thanks a lot for your help.

Answer

Here is something you may be able to work a solution off, assuming the response is one full string with the same terms in each response, you have a pre-defined 'needle' array to look through and it splits off the string in between the two needles into its own array. The two needles in this case are two terms to look for, such as the 'domain:' and 'native_domain:'.

<?php

$responseStr = "domain: xxx.de native_domain: xxx.de user: master status: active transfer_lock: yes auto_renew: yes delegation: yes ownerc: ASDASD1 adminc: ASDASD1 techc: ASDASD1 billc: FLOSCH2 created: 2015-08-25 10:31:08 modified: 2016-08-20 16:20:15 expire: 2017-08-25 10:31:13 chreseller_auth: dom-cdmv-zs0f-mpwg auth_info: dns::1/hostname: dns1.xxx.com dns::1/hostip: dns::2/hostname: dns2.xxx.com dns::2/hostip: dns::3/hostname: dns::3/hostip: dns::4/hostname: dns::4/hostip: dns::5/hostname: dns::5/hostip: comment: inserted: 2016-01-29 23:46:29";

$responseArray = [
    'domain:',
    'native_domain:',
    'user:',
    'status:',
    'transfer_lock:',
    'auto_renew:',
    'delegation:',
    'ownerc:',
    'adminc:',
    'techc:',
    'billc:',
    'created:',
    'modified:',
    'expire:',
    'chreseller_auth:',
    'auth_info:',
    'dns::1/hostname:',
    'dns::1/hostip:',
    'dns::2/hostname:',
    'dns::2/hostip:',
    'dns::3/hostname:',
    'dns::3/hostip:',
    'dns::4/hostname:',
    'dns::4/hostip:',
    'dns::5/hostname:',
    'dns::5/hostip:',
    'comment:',
    'inserted:',
    "\r"
];


function get_string_between($string, $start, $end){
    $string = " ".$string;
    $ini = strpos($string,$start);
    if ($ini == 0) return "";
    $ini += strlen($start);   
    $len = strpos($string,$end,$ini) - $ini;
    return substr($string,$ini,$len);
}

for ($i = 0; $i < sizeof($responseArray) - 1; $i++) {
    $finalArr[$responseArray[$i]] = trim(get_string_between($responseStr, $responseArray[$i], $responseArray[$i+1]));
}
?>

<pre>
<?php var_dump($finalArr); ?>
</pre>

This will give the following output;

array(28) {
  ["domain:"]=>
  string(6) "xxx.de"
  ["native_domain:"]=>
  string(6) "xxx.de"
  ["user:"]=>
  string(6) "master"
  ["status:"]=>
  string(6) "active"
  ["transfer_lock:"]=>
  string(3) "yes"
  ["auto_renew:"]=>
  string(3) "yes"
  ["delegation:"]=>
  string(3) "yes"
  ["ownerc:"]=>
  string(7) "ASDASD1"
  ["adminc:"]=>
  string(7) "ASDASD1"
  ["techc:"]=>
  string(7) "ASDASD1"
  ["billc:"]=>
  string(7) "FLOSCH2"
  ["created:"]=>
  string(19) "2015-08-25 10:31:08"
  ["modified:"]=>
  string(19) "2016-08-20 16:20:15"
  ["expire:"]=>
  string(19) "2017-08-25 10:31:13"
  ["chreseller_auth:"]=>
  string(18) "dom-cdmv-zs0f-mpwg"
  ["auth_info:"]=>
  string(0) ""
  ["dns::1/hostname:"]=>
  string(12) "dns1.xxx.com"
  ["dns::1/hostip:"]=>
  string(0) ""
  ["dns::2/hostname:"]=>
  string(12) "dns2.xxx.com"
  ["dns::2/hostip:"]=>
  string(0) ""
  ["dns::3/hostname:"]=>
  string(0) ""
  ["dns::3/hostip:"]=>
  string(0) ""
  ["dns::4/hostname:"]=>
  string(0) ""
  ["dns::4/hostip:"]=>
  string(0) ""
  ["dns::5/hostname:"]=>
  string(0) ""
  ["dns::5/hostip:"]=>
  string(0) ""
  ["comment:"]=>
  string(0) ""
  ["inserted:"]=>
  string(0) ""
}