donglun4682 2019-07-19 14:45
浏览 340
已采纳

php - 如何使用等号=符号的字符串值从数组中提取值而不使用preg_match

I have received a response from server as a string, which I converted as array, but still I am not able to make out, how I can access values from this array like

myarrray['txn_status'];

as also from the string clnt_rqst_meta

array(13) {
  [0]=>
  string(15) "txn_status=0399"
  [1]=>
  string(15) "txn_msg=failure"
  [2]=>
  string(55) "txn_err_msg=Transaction Cancelled : ERROR CODE TPPGE161"
  [3]=>
  string(17) "clnt_txn_ref=9178"
  [4]=>
  string(15) "tpsl_bank_cd=NA"
  [5]=>
  string(19) "tpsl_txn_id=T245107"
  [6]=>
  string(14) "txn_amt=121.00"
  [7]=>
  string(47) "clnt_rqst_meta={mob:9937253528}{custname:pawan}"
  [8]=>
  string(16) "tpsl_txn_time=NA"
  [9]=>
  string(15) "tpsl_rfnd_id=NA"
  [10]=>
  string(10) "bal_amt=NA"
  [11]=>
  string(47) "rqst_token=cd3f6f55-5990-4c3b-bb12-238eede827a0"
  [12]=>
  string(45) "hash=3cf25909ec73865d3200bc267119d3fcc21df463"
}

I know that the same can be achieved using regex/preg_match, but I am sure there must be some straight forward way to achieve it.

update: the actual string received from response is like this:

string(342) "txn_status=0399|txn_msg=failure|txn_err_msg=Transaction Cancelled : ERROR CODE TPPGE161|clnt_txn_ref=9178|tpsl_bank_cd=NA|tpsl_txn_id=T245107|txn_amt=121.00|clnt_rqst_meta={mob:9937253528}{custname:pawan}|tpsl_txn_time=NA|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=cd3f6f55-5990-4c3b-bb12-238eede827a0|hash=3cf25909ec73865d3200bc267119d3fcc21df463" 

so I used $response =explode("|",$response_str);

  • 写回答

5条回答 默认 最新

  • dongliugu8843 2019-07-19 14:59
    关注

    You could let the PHP function parse_str() do the heavy lifting for you but it expects to receive a standard query string, with the entries separated by &. Your input string uses a different separator (|) and parse_str() does not provide a way to tell it what character to use as separator.

    The problem has a very simple solution: use str_replace() to replace | to & in the input string then pass the result to parse_str():

    $input = 'txn_status=0399|txn_msg=failure|txn_err_msg=Transaction Cancelled : ERROR CODE TPPGE161|clnt_txn_ref=9178|tpsl_bank_cd=NA|tpsl_txn_id=T245107|txn_amt=121.00|clnt_rqst_meta={mob:9937253528}{custname:pawan}|tpsl_txn_time=NA|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=cd3f6f55-5990-4c3b-bb12-238eede827a0|hash=3cf25909ec73865d3200bc267119d3fcc21df463';
    parse_str(str_replace('|', '&', $input), $output);
    
    print_r($output);
    

    It produces:

    Array
    (
        [txn_status] => 0399
        [txn_msg] => failure
        [txn_err_msg] => Transaction Cancelled : ERROR CODE TPPGE161
        [clnt_txn_ref] => 9178
        [tpsl_bank_cd] => NA
        [tpsl_txn_id] => T245107
        [txn_amt] => 121.00
        [clnt_rqst_meta] => {mob:9937253528}{custname:pawan}
        [tpsl_txn_time] => NA
        [tpsl_rfnd_id] => NA
        [bal_amt] => NA
        [rqst_token] => cd3f6f55-5990-4c3b-bb12-238eede827a0
        [hash] => 3cf25909ec73865d3200bc267119d3fcc21df463
    )
    

    See it in action: https://3v4l.org/KBaof

    Warning

    The solution exposed above works fine only if the input string does not contain & and %. These characters are special in query strings, parse_str() tries to interpret them using their special meaning and the code above breaks.

    A solution that works when & or % is present in the input string

    $input = 'txn_status=0399|txn_msg=failure|txn_err_msg=Transaction Cancelled : ERROR CODE TPPGE161|clnt_txn_ref=9178|tpsl_bank_cd=NA|tpsl_txn_id=T245107|txn_amt=121.00|clnt_rqst_meta={mob:9937253528}{custname:pawan}|tpsl_txn_time=NA|tpsl_rfnd_id=NA|bal_amt=NA|rqst_token=cd3f6f55-5990-4c3b-bb12-238eede827a0|hash=3cf25909ec73865d3200bc267119d3fcc21df463';
    $output = array_reduce(
        explode('|', $input),
        function($acc, $item) {
            list($key, $value) = explode('=', $item, 2);
            $acc[$key] = $value;
            return $acc;
        },
        []
    );
    
    print_r($output);
    

    It still fails if | is present in the values (e.g. ...|txn_err_msg=a|b|clnt_txn_ref=...) but this issue doesn't have a solution because of the naive encoding of the input string.

    If you can modify the server that produces the input data, change it to produce JSON output. JSON is a format that allows validation of the input string and PHP provides functions to encode and decode it. The code becomes cleaner on both sides.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?