PHP SDK: Node: An Individual Card or Element
$card is returned by $dom->q(...) or is an item from $dom->qa(...). First check that the search returned an object rather than null. An algorithm cannot create a Node with new.
Methods
text
text(bool $trim = true): string
Get the found element's text. Repeated spaces and line breaks are combined.
$name = $card->text();
Result and notes. Returns a string. With trim=false, outer whitespace is retained, but internal spaces are still normalized.
html
html(): string
Get the whole element's HTML, including its own tag.
$fragment = $card->html();
Result and notes. Returns a string containing the matched markup, not a byte-for-byte copy of the source document.
attr
attr(string $name): ?string
Get the value of one attribute.
$sku = $card->attr('data-sku');
Result and notes. Returns a trimmed string or null if the attribute is absent or empty.
abs
abs(string $attribute = 'href'): ?string
Get an absolute URL from an attribute. href is used by default.
$imageUrl = $card->q('img')?->abs('src');
Result and notes. Returns a string or null. Set a base URL when creating the Dom; without one, a relative link remains relative.
q
q(string $css): ?Node
Find the first matching element inside the current fragment.
$price = $card->q('.price')?->text();
Result and notes. Returns a Node or null. Searching within the current card prevents mixing data from adjacent products.
qa
qa(string $css): array
Get all matches inside the current fragment.
$images = [];
foreach ($card->qa('img') as $image) { $images[] = $image->abs('src'); }
Result and notes. Returns a list of Nodes, or an empty list when nothing matches.
exists
exists(): bool
Check that an obtained Node exists.
$node = $dom->q('h1');
$found = $node !== null && $node->exists();
Result and notes. An existing Node always returns true. To check a search result, $node !== null is enough. This method cannot be called on null.