public function parseResults($resString)
{
// Raw tucows result
$resArray = explode(".\r\n", $resString);
$resRinse = array();
for ($i = 0; $i < count($resArray); ++$i) {
// Clean up \n, \r and empty fields
$resArray[$i] = str_replace("\r", '', $resArray[$i]);
$resArray[$i] = str_replace("\n", ' ', $resArray[$i]);
// replace new line with space
$resArray[$i] = str_replace(' ', ' ', $resArray[$i]);
// no double space - for further parsing
$resArray[$i] = substr($resArray[$i], 0, -1);
// take out the last space
if ($resArray[$i] != '') {
array_push($resRinse, $resArray[$i]);
}
}
$result = array('is_success' => '1', 'response_code' => '200', 'response_text' => 'Command completed successfully');
$i = 1;
// Takes the rinsed result lines and forms it into an Associative array
foreach ($resRinse as $resultLine) {
$okPattern = '/^OK 0/';
$arrayPattern = '/ ([\\w\\-\\.\\@]+)\\=\\"([\\w\\-\\.\\@\\*\\, ]*)\\"/';
$errorPattern = '/^ER ([0-9]+) (.+)$/';
// Checks to see if this line is an information line
$okLine = preg_match($okPattern, $resultLine, $matches);
if ($okLine == 0) {
// If it's not an ok line, it's an error
$err_num_match = 0;
$err_num_match = preg_match($errorPattern, $resultLine, $err_match);
// Makes sure the error pattern matched and that there isn't an error that has already happened
if ($err_num_match == 1 && $result['is_success'] == '1') {
$result['response_text'] = $err_match[2];
$result['response_code'] = $err_match[1];
$result['is_success'] = '0';
}
} else {
// If it's an OK line check to see if it's an Array of values
$arrayMatch = preg_match_all($arrayPattern, $resultLine, $arrayMatches);
if ($arrayMatch != 0) {
for ($j = 0; $j < $arrayMatch; ++$j) {
if ($arrayMatches[1][$j] == 'LIST') {
$result['attributes'][strtolower($arrayMatches[1][$j])] = explode(',', $arrayMatches[2][$j]);
} else {
$result['attributes'][strtolower($arrayMatches[1][$j])] = $arrayMatches[2][$j];
}
}
} else {
// If it's not an array line or an error it could be a table
$tableLines = explode(' , ', $resultLine);
if (count($tableLines) > 1) {
$tableLines[0] = str_replace('OK 0 ', '', $tableLines[0]);
$tableHeaders = explode(' ', $tableLines[0]);
$result['attributes']['list'] = array();
for ($j = 1; $j < count($tableLines); ++$j) {
$values = explode('" "', $tableLines[$j]);
$k = 0;
foreach ($tableHeaders as $tableHeader) {
$result['attributes']['list'][$j - 1][strtolower($tableHeader)] = str_replace('"', '', $values[$k]);
++$k;
}
}
}
}
}
++$i;
}
return $result;
}