-1

I need to split words after equals (=), output is following:

servercreate virtualserver_port=5383 virtualserver_maxclients=5
sid=43 token=hXy4fvF54hMgcZTJkf6f1JcPpvkURMDiOIJ9ERqN virtualserver_port=5383
error id=0 msg=ok

I need this sid=NUMBER and token=hXy4... to split in php and store in mysql.

I tried myself to split with:

$sid = split("sid=", $data);
    $token = split("token=", $sid[1]);
    fclose($stream);
    $data1 = ["0" => "$sid[0]", "1" => "$token[1]"];
    return $data1;

but as return I receive this:

TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.

Basically there is three commands running in php with ssh2 function, first it was "telnet IP PORT" and output for that is:

TS3
Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help <command>" for information on a specific command.

Next command is "login ..." output is:

error id=0 msg=ok

and latest one is ""servercreate ..." and output is sid=... token=...

Tronyx
  • 1

2 Answers2

0

Use a regex:

$data = "servercreate virtualserver_port=5383 virtualserver_maxclients=5
sid=43 token=hXy4fvF54hMgcZTJkf6f1JcPpvkURMDiOIJ9ERqN virtualserver_port=5383
error id=0 msg=ok";

if (preg_match_all("/(\w+)=(\w+)/", $data, $matches)) {
    var_dump(array_combine($matches[1], $matches[2]));
}

Sample output:

array(6) {
  ["virtualserver_port"]=>
  string(4) "5383"
  ["virtualserver_maxclients"]=>
  string(1) "5"
  ["sid"]=>
  string(2) "43"
  ["token"]=>
  string(40) "hXy4fvF54hMgcZTJkf6f1JcPpvkURMDiOIJ9ERqN"
  ["id"]=>
  string(1) "0"
  ["msg"]=>
  string(2) "ok"
}
Emil H
  • 39,840
  • 10
  • 78
  • 97
-1

Use explode() and be creative with php string functions:

$explodeOnEqualSign = explode("=", $str);
$count = count($explodeOnEqualSign);

$sid = $token = '';
for($i = 0; $i < $count; $i++) {

    $previous = $i - 1;
    if (substr($explodeOnEqualSign[$previous], -3) === 'sid') {
        $sid = strstr($explodeOnEqualSign[$count], " ", true);
    } else {
        if (substr($explodeOnEqualSign[$previous], -5) === 'token') {
            $token = strstr($explodeOnEqualSign[$count], " ", true);
        }
    }

}

return array($sid, $token);