D
D
Dmitry TukTuk2021-11-25 11:54:54
PHP
Dmitry TukTuk, 2021-11-25 11:54:54

How to parse javascript in phpqueryOjbect?

How to parse data from phpqueryOjbect which has <script>...<script>?
I have a product page, I need to parse breadcrumbs in it, phpqueryOjbect does not contain selectors in html through which they can be obtained, the product category is stored in a js script. Here is the script itself that is in the object -

<script>
                window.dataLayer = window.dataLayer || [];
                function gtag(){dataLayer.push(arguments);}
                gtag('js', new Date());
                gtag('config', 'AW-825966386');
                    var payload = {
                        'send_to': 'AW-825966386',
                        'ecomm_pagetype': 'product',
                            'ecomm_prodid': '739330095',
                            'ecomm_totalvalue': 55.81,
                            'ecomm_category': 'Сосна искусственная'
                    };
                    gtag('event', 'page_view', payload);
            </script>


I need to get 1 element from the script, namely ecomm_category and its values.

Here is my setup and logic
//phpQueryObject
    $productCard = pq($productCards);
    //пустой массив крошек
    $data['breadcrumbs'] = array();

    $entry = $productCard->find('');//Обратиться к элементам из скрипта js

    //Создание массива с категориями
    foreach ($entry as $row) {
        $ent = pq($row);
        $name = $ent->text();
        $url = $ent->attr('href');
        $data['breadcrumbs'][$name] = $url;
    }
    print_r($data['breadcrumbs']);

I wanted to get the breadcrumb data into a key value array, and use the penultimate element (the product category, since the last element on the site is the product name) to output the category to the xml output.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-11-25
@tuktuk-Laravel

Here is a regular expression for you, for example: https://regex101.com/r/BHfpVi/1/
But you write something strange. You want to pull out ecomm_category from js, but in the code it looks like it's a search of some links.
In general, phpQuery is ancient and inconvenient crap, and wouldn't it be time to switch to DiDom , symfony/dom-crawler , or laminas-dom ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question