美文网首页
convert js object to json in php

convert js object to json in php

作者: mihope | 来源:发表于2019-01-16 09:06 被阅读1次
<?php
$url = 'https://www.cbex.com.cn/xm/cqzr/201901/t20190107_26742.html';
$html = file_get_contents($url);
$p = '/<textarea[^>]*?>(.*?)<\/textarea>/s';

$strHtml= '/<div[^>]*?>(.*)<\/div>/s';

preg_match($p, $html, $match);
$str = $match[1];
function replaceUnnecessary($strJs)
{
    $str = str_replace("&quot;", "", $strJs);
    $str = str_replace("{", "{\"", $str);
    $str = str_replace(":", "\":\"", $str);
    $str = str_replace(":\"{", ":{", $str);
    $str = str_replace(",", "\",\"", $str);
    $str = str_replace("}\",", "},", $str);
    $str = str_replace("},", "\"},", $str);
    $str = str_replace("\":\"{", "\":{", $str);
    $str = str_replace("]\"", "]", $str);
    $str = str_replace("\"[", "[", $str);
    $str = str_replace("}]", "\"}]", $str);
    return str_replace("}}", "\"}}", $str);
}

function httPreg($matches)
{
    $sub = str_replace("\"", "", $matches[1]);
    return "\":\"" . $sub . "\",\"";
}

$str = preg_replace_callback('/\":\"([^\{\}\]\[]*?)\",\"/', "httPreg", replaceUnnecessary($str));
$jsonData = json_decode($str);

//标的企业名称
echo $jsonData->utrgcemspreobject->objectname . "\n";
//法定代表人
echo $jsonData->utrgcemspreobject->objectboss . "\n";
//注册资本
echo $jsonData->utrgcemspreobject->registeredcapital . "\n";
//经济类型
echo $jsonData->utrgcemspreobject->economytype . "\n";
//企业类型
echo $jsonData->utrgcemspreobject->economynature . "\n";
//经济规模
echo $jsonData->utrgcemspreobject->managerscale . "\n";
//经营范围
echo $jsonData->utrgcemspreobject->businessscope . "\n";

相关文章

网友评论

      本文标题:convert js object to json in php

      本文链接:https://www.haomeiwen.com/subject/srepdqtx.html