代码之家  ›  专栏  ›  技术社区  ›  ForgivenIT

将HTML响应转换为PHP数组

  •  0
  • ForgivenIT  · 技术社区  · 7 年前

    json_decode parse_str 但我无法获取数组中要返回的值。

    我要做的是输出一个包含名称、价格和url的数组。报税表的格式让我很为难(我准确地复制了它,没有错别字或缺少‘字符’)。

    $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 回复  |  直到 7 年前
        1
  •  1
  •   ryantxr    7 年前

    服务返回的JSON错误。看起来你可以 假设没有单引号 在任何数据值中 .

    $tmp=str\u replace('gSellPrices=','',$result408); $tmp=str\u 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:…'}];
    
        2
  •  0
  •   user9300102 user9300102    7 年前

    你在找json解码( http://php.net/manual/tr/function.json-decode.php )

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