John Davidson

php - how to get the key value pair from a line in a file

0 comments
Message:


Hello. I have this line extracted from a file.


2021-03-08 13:44:11,241  INFO [192.168.34.10] - response data ==> {
"result": {
"vResultCode": "N001000001100000",
"orderInfos": {
"orderInfo": [
{
"index": 0,
"lastSuccessTxnType": "Authorize",
"orderId": "9040000123",
"orderStatus": "end",
"properOrderInfo": {
"cardExpire": "000",
"startTxn": "60979460"
},
"serviceTypeCd": "card",
"successDetailTxnType": "a",
"transactionInfos": {
"transactionInfo": [
{
"amount": "560",
"command": "Authorize",
"mstatus": "success",
"properTransactionInfo": {
"cardTransactionType": "a",
"centerRequestDate": "20210308134240",
"centerResponseDate": "20210308134240",
"connectedCenterId": "jcn",
"gatewayRequestDate": "20210308134240",
"gatewayResponseDate": "20210308134240",
"loopback": "0",
"pending": "0",
"reqAcquirerCode": "05",
"reqAmount": "560",
"reqCardExpire": "*****",
"reqCardNumber": "411111*11",
"reqItemCode": "0990",
"reqJpoInformation": "10",
"reqSecurityCode": "000",
"reqWithCapture": "false",
"resActionCode": "000",
"resAuthCode": "000000",
"resCenterErrorCode": " ",
"resReturnReferenceNumber": "012345678901",
"txnKind": "card"
},
"txnDatetime": "2021-03-08 13:42:40.285",
"txnId": "60979460",
"vResultCode": "A001000000000000"
}
]
}
}
]
},
"overMaxCountFlag": false,
"searchCount": 1,
"mstatus": "success",
"serviceType": "search"
}
}

How do i get the key value pairs from here? like i want to get the reqAmount which is '560' or the mstatus that is 'success' I'm using php laravel btw**



My Answer:
To extract key-value pairs from a line in a file in PHP, you can use regular expressions to match the key-value pairs in the line. Here's an example code snippet that demonstrates how to extract key-value pairs from the given line:

php

$line = '2021-03-08 13:44:11,241 INFO [192.168.34.10] - response data ==';

// Define a regular expression pattern to match key-value pairs
$pattern = '/(\w+)\s*==\s*(\S+)/';

// Match key-value pairs in the line using the regular expression
preg_match_all($pattern, $line, $matches, PREG_SET_ORDER);

// Extract and display the key-value pairs
foreach ($matches as $match) {
$key = $match[1];
$value = $match[2];
echo "Key: $key, Value: $value\n";
}
?>


In this code snippet, we define a regular expression pattern `(\w+)\s*==\s*(\S+)` to match key-value pairs in the line. The pattern matches a word character (key) followed by zero or more whitespace characters, followed by '==', followed by zero or more whitespace characters, followed by a non-whitespace character (value).

We then use `preg_match_all()` function to match all key-value pairs in the line using the regular expression pattern. The matched key-value pairs are stored in the `$matches` array.

Finally, we iterate over the matched key-value pairs in the `$matches` array and extract the key and value from each match, and display them.

You can modify the regular expression pattern according to the specific format of key-value pairs in your file.

Rate this post

4 of 5 based on 1281 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us