Search code examples
phpexplodestrpos

PHP Finding Data in Dynamic String


So, in short I have an issue I am facing where a bug in some code caused records to not be inserted into a database even though the UI told it that it was submitted (stupid checking on my part).

One thing we do though is log all the errors that are returned by the database so we can see any time a record isn't inserted and the reason why.

With this, I have all 7,200 records that need to be re-inserted into the database now that the issue has been fixed in the stored procedure.

This is how we stored the parameters that were being sent over:

xml=<data><optional><Account>1551573750449311384</Account></optional></data>, submitter=Q10004370, target=Q10224247, escalationType=aal, escalationReason=253, feedback= , preventable=1

I am going to loop over all of this data in my error log and try and prepare all of the data to re-insert back into the table. My issue is though that the string could contain less or more data; it's dynamic.

I need a way where I can search for the specific parts of the string such as xml or target.

That will allow me to set up all the data with the fields they need to be re-inserted into.

I first started with PHP's explode on the commas but realized that things are never in the same order. I then was going to try and use strpos to get the position and then get the value but had trouble doing so.

How can I go about getting the values (piece that precedes the = sign and is before the comma?

End Result is to be able to get each of the variables I need from this string so I can pass them to an insert statement to put them back into the database.

Update: While this is working, my issue now is that there could be commas in the feedback= section and its causing issues with the array.

    $array = Array();

// Loop over each one of the records we are going to be working with
foreach($data->data as $p){

    // Create an array of all the params
    $pieces = explode(", ", rtrim($p->params, ", "));

    // Debug to show all of the records we are using
    /*
    print '<pre>';
    print_r($p);
    print '</pre>';
    */

    // Loop over all the params which is defined by the = sign
    foreach($pieces as $item) {
        // Put the values of each of the results into an array
        $result[] = explode("=", trim($item));
    }

    // Loop over all of the results in the array
    foreach($result as $item) {
        // Store the param name as the key in our end result
        $end[trim($item[0])] = $item[1];
    }

    // Push the final arrays into the results array
    array_filter($end);
    array_push($array, $end);
}

// Display the end results  
print "<pre>";
print_r($array);
print "</pre>";

Solution

  • Maybe like this?

    $str    = 'xml=<data><optional><Account>1551573750449311384</Account></optional></data>, submitter=Q10004370, target=Q10224247, escalationType=aal, escalationReason=253, feedback= , preventable=1';
    $foo    = explode(",", $str);
    
    foreach($foo as $item) {
    
        $result[] = explode("=", $item);
    
    }
    
    
    foreach($result as $item) {
    
        $end[$item[0]] = $item[1] ;
    
    }
    
    echo "<pre>";
    print_r($end);
    echo "</pre>";
    

    Live link: http://sandbox.onlinephpfunctions.com/code/37aa046be2e9df37eff015ad292cd8a794a82782