drrog9853 2018-09-17 14:07
浏览 164
已采纳

将HTML响应转换为PHP数组

The response I am getting from a web call in php is something i have never seen. I have tried json_decode function and I have also tried the parse_str below, but I can't get the values to return in an array.

What i want to do is to output an array with name, price, and url. The formatting of the return has got me stumped (I copied it exactly, no typos or missing ' characters).

$urltest='https://www.***';
$result408 = file_get_contents($urltest);
//parse_str($result408, $output);

Prices = [{'name':'Seller1', 'unknown':'unknown0',  'price':60.37, 'price_f':'$60<sup style="font-size:12px;">37</sup>', 'url':'http:…'},
        {'name':'Seller2', 'unknown':'unknown0','price':87.25, 'price_f':'$87<sup style="font-size:12px;">25</sup>, 'url':'http:…'},
        {'name':'Seller3', 'unknown':'unknown0',  'price':74, 'price_f':'$74<sup style="font-size:12px;">00</sup>', 'url':'http:…'}];
  • 写回答

2条回答 默认 最新

  • dongxi2014 2018-09-17 15:37
    关注

    The JSON returned from the service is bad. It looks like you can alter it to make it into JSON that can be parsed. Bear in mind this approach assumes that there are no single quotes in any of the data values.

    $tmp=str_replace('gSellPrices =', '',$result408); $tmp = str_replace('"', '\"', $tmp); $tmp = str_replace('\'', '"', $tmp); $tmp = rtrim($tmp, '; ');

    $urltest='https://www.***';
    $result408 = file_get_contents($urltest);
    // Remove 'Prices = '
    $tmp=str_replace('gSellPrices =', '',$result408);
    // Protect the existing double quotes
    $tmp = str_replace('"', '\\"', $tmp);
    // Now replace all the incorrect single quotes with double quotes.
    $tmp = str_replace('\'', '"', $tmp);
    // Remove trailing semicolon
    $tmp = rtrim($tmp, '; ');
    // Now we can json_decode
    $data = json_decode($tmp);
    
    
    Assuming that the output from service is EXACTLY as follows:
    
    Prices = [{'name':'Seller1', 'unknown':'unknown0',  'price':60.37, 'price_f':'$60<sup style="font-size:12px;">37</sup>', 'url':'http:…'},
        {'name':'Seller2', 'unknown':'unknown0','price':87.25, 'price_f':'$87<sup style="font-size:12px;">25</sup>, 'url':'http:…'},
        {'name':'Seller3', 'unknown':'unknown0',  'price':74, 'price_f':'$74<sup style="font-size:12px;">00</sup>', 'url':'http:…'}];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用