Answer the question
In order to leave comments, you need to log in
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>
//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']);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question