-1

Hoping you can help me here. I am at a bit of a loss :/

I have a function that passes data via API to a server. It sends a response. I need to split the string that comes in and take the final result and make that a variable that can be used by future functions.

This is the result from the server once I have successfully passed the data

CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975

This is shown by

echo($add_client_response->AddClientKYCResult);

From this I need to get the numbers only of the CLIENTID and strip everything else out.

I have this snippet of code so far that isn't working

function AddClientKYCResult ( $data, $pilot ) {
$add_client_response = preg_split('/;/', $result['Result']);
$clientId = preg_split('/=/', $spl[0][0]);
$clientRef = preg_split('/=/', $spl[1][0]);
$contactId = preg_split('/=/', $spl[2][0]);
return $data;
echo($clientId);
}

Can someone help on this one?

TIA

2 Answers2

0

Manually, this can be done in a dirty way using explode, or a bit nicer with the common stack of strpos, strlen and substr.

<?PHP
    $string = "CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975";

    // approach #1 use explode
    // dirty, since it needs clientID to be the first parameter
    $split = explode(";",$string);
    $finalResult = str_replace("CLIENTID=","",$split[0]);
    var_dump($finalResult);

    // approach #2 use strpos
    // better if you want to handle errors
    $beginClientID = strpos($string,"CLIENTID=");
    $endClientID = strpos($string,";",$beginClientID);
    $clientID = substr($string,$beginClientID+strlen("CLIENTID="),$endClientID-($beginClientID+strlen("CLIENTID=")));
    var_dump($clientID);



?>
maio290
  • 6,440
  • 1
  • 21
  • 38
0

To take in cognizance that CLIENTID can be anywhere in the string.

$apiResponse = $add_client_response->AddClientKYCResult;
//$apiResponse = "CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975";

$add_client_response = explode(";",$apiResponse);
$result = [];
foreach($add_client_response as $part) {
      $partArr = explode("=", $part);      
      $result[$partArr[0]] = $partArr[1];
}

$clientId = $result["CLIENTID"];
$clientRef = $result["CLIENTREF"];
$contactId = $result["CONTACTID"];
echo("clientId = $clientId<br/>");
echo("clientRef = $clientRef<br/>");
echo("contactId = $contactId");
Tope Omotunde
  • 40
  • 2
  • 9
  • ($add_client_response->AddClientKYCResult) - This is where the result comes from, how would I incorporate that into your code - this one looks the best. I have tested it and using this string it works super! But I'll need it for each result from the result – Andrew Royal Dec 10 '18 at 13:43
  • Made changes to the code to accommodate any use case. – Tope Omotunde Dec 10 '18 at 13:53
  • Thanks Tope, the code is perfect, but I need to pull from the $add_client_response result that is passed back from the database. in your $result['Result'] = "CLIENTID=101410;CLIENTREF=MZABOX2382;CONTACTID=22975"; it uses the static result from one response. Is there a way to get it each time from this - ($add_client_response->AddClientKYCResult)... The fact that there is a $clientId is perfect – Andrew Royal Dec 10 '18 at 14:00
  • Adjusted to take result from the $add_client_response->AddClientKYCResult – Tope Omotunde Dec 10 '18 at 14:07
  • Super, thanks.. but now I get Warning: Illegal string offset when I run it with the return from the database, but if I run your commented out line it works fine. Also needed to add in a ; for the first line – Andrew Royal Dec 10 '18 at 14:16
  • Please, var_dump($add_client_response->AddClientKYCResult), so I can see you full detail and process it correctly. – Tope Omotunde Dec 10 '18 at 14:23
  • string(52) "CLIENTID=101428;CLIENTREF=MZABOX2400;CONTACTID=22993" this is what I am getting – Andrew Royal Dec 10 '18 at 14:28
  • Adjusted, I assumed that $add_client_response->AddClientKYCResult was an array, it is actually the string, we are processing. This should work. – Tope Omotunde Dec 10 '18 at 14:33
  • You sir are a scholar and a gentleman! Thank you so much for your help! – Andrew Royal Dec 10 '18 at 14:35