dongqie2010 2017-10-04 10:34
浏览 231
已采纳

从curl响应字符串中获取特定变量

I am getting the response from curl as follows in a string:

Accepted=AVSAUTH:TEST:::829649376:N::U
ENTRYMETHOD=KEYED
historyid=829649376
MERCHANTORDERNUMBER=14700000186
orderid=646526156
PAYTYPE=MasterCard
recurid=0
refcode=829649376-TEST
result=1
Status=Accepted
transid=0

I want to get individual variables like status= Accepted so that i can able to store those in my database. Any help will be highly appreciable.

  • 写回答

3条回答 默认 最新

  • dqwr32867 2017-10-04 10:59
    关注

    preg_match is your friend:

    <?
    $result = "Accepted=AVSAUTH:TEST:::829649376:N::U
    ENTRYMETHOD=KEYED
    historyid=829649376
    MERCHANTORDERNUMBER=14700000186
    orderid=646526156
    PAYTYPE=MasterCard
    recurid=0
    refcode=829649376-TEST
    result=1
    Status=Accepted
    transid=0";
    
    preg_match_all("/^([^=]+)=(.*)$/m", $result, $regs, PREG_SET_ORDER);
    
    $data = [];
    foreach($regs as $reg) {
        $data[$reg[1]] = $reg[2];
    }
    
    print_r($data);
    ?>
    

    $data will be an hash Array:

    Array
    (
        [Accepted] => AVSAUTH:TEST:::829649376:N::U
        [ENTRYMETHOD] => KEYED
        [historyid] => 829649376
        [MERCHANTORDERNUMBER] => 14700000186
        [orderid] => 646526156
        [PAYTYPE] => MasterCard
        [recurid] => 0
        [refcode] => 829649376-TEST
        [result] => 1
        [Status] => Accepted
        [transid] => 0
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥200 基于同花顺supermind的量化策略脚本编辑
  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?