Parse JSON data inside a Complex loop using SimpleHtmlDom

90 Views Asked by At

I want to display the contents of a json script with the help of simple HTML Dom and my goal is to display the second "@type": "user" name and url feed:

my Json :

<script type="application/ld+json">
              
              "type": {
                "@type": "Type",
                "name": "admin"
              },
              "offers": {
                "@type": "AggregateOffer",
                "offerCount": "30"
                ,"offers": [
            {
              "@type": "user",
            "name": "abc",
            "url": "https://test.com",
            },{
                "@type": "user",
            "name": "eds",
            "url": "https://example.com",
            },{
                "@type": "user",
            "name": "gfh",
            "url": "https://test.com",
            },{
                "@type": "user",
            "name": "dfc",
            "url": "https://test.com",
            },
            .
            .
            .

My desired output: "name": "eds", "url": "https://example.com"

For this I am using following code:

$json = $html->find('script[type="application/ld+json"]',0);
echo $json->innertext;

But this code shows me all the json contents, I really don't know how to make a condition in my code that only the second "@type": "user" name and url feed from this complex cycle will be displayed to me

1

There are 1 best solutions below

9
IT goldman On

Assuming your HTML and JSON are valid:


$html = '
<body>
<h1>hello </h1>
<script type="application/ld+json">{
    "type": {
        "@type": "Type",
        "name": "admin"
    },
    "offers": {
        "@type": "AggregateOffer",
        "offerCount": "30"
        ,"offers": [
            {
            "@type": "user",
            "name": "abc",
            "url": "https://test.com"
            },{
            "@type": "user",
            "name": "eds",
            "url": "https://example.com"
            },{
            "@type": "user",
            "name": "gfh",
            "url": "https://test.com"
            },{
            "@type": "user",
            "name": "dfc",
            "url": "https://test.com"
            }
        ]
    }
}   
</script>';

$dom = new DomDocument(); 
$dom->loadHTML($html); 
$node= $dom->getElementsByTagName("script")[1];
$text= $node->textContent;
$obj = json_decode($text, true);
echo $obj["offers"]["offers"][1]["name"];
echo $obj["offers"]["offers"][1]["url"];

UPDATE: There are other errors in the page that prevent the parser from working. I suggest using a function to extract the script tag text.

function after($this1, $inthat)
{
    if (!is_bool(strpos($inthat, $this1))) {
        return substr($inthat, strpos($inthat, $this1) + strlen($this1));
    }
    return null;
}

function before($this1, $inthat)
{
    return substr($inthat, 0, strpos($inthat, $this1));
}

function between($this1, $that, $inthat)
{
    return before($that, after($this1, $inthat));
}

$text = between('<script type="application/ld+json">', '</script>', $html);
// then continue like before.