Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined array key 0 after array_filter #96

Open
mihaistana opened this issue Nov 27, 2024 · 3 comments
Open

Undefined array key 0 after array_filter #96

mihaistana opened this issue Nov 27, 2024 · 3 comments

Comments

@mihaistana
Copy link

mihaistana commented Nov 27, 2024

After the array_filter on line 1481 on function hasSingleTagInsideElement the array sometimes not start with 0,

ErrorException

Undefined array key 0

at vendor/j0k3r/php-readability/src/Readability.php:1485
1481▕ $children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
1482▕ //$children = array_values($children);
1483▕ // There should be exactly 1 element child with given tag
1484▕
➜ 1485▕ if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
1486▕ return false;
1487▕ }
1488▕
1489▕ $a = array_filter(

to fix it you have to add array_values to reset the array index.

private function hasSingleTagInsideElement(\DOMElement $node, string $tag): bool
{
$childNodes = iterator_to_array($node->childNodes);
$children = array_filter($childNodes, fn ($childNode) => $childNode instanceof \DOMElement);
$children = array_values($children);
// There should be exactly 1 element child with given tag
if (1 !== \count($children) || $children[0]->nodeName !== $tag) {
return false;
}

@j0k3r
Copy link
Owner

j0k3r commented Nov 27, 2024

You might be right. If you can reproduce the bug with a given website & create a test, I'm happy to review the fix :)

@mihaistana
Copy link
Author

Hi @j0k3r yes you can try with my website "https://agenciaweb.net" it's where I tried it and failed.

@ofeige
Copy link

ofeige commented Jan 10, 2025

For example with all pages from golem.de

$url = 'https://www.golem.de/news/anzeige-varta-aa-batterien-grosses-set-zum-kleinen-preis-bei-amazon-2501-192329.html';

$graby = new Graby();
$result = $graby->fetchContent($url);

@ofeige ofeige mentioned this issue Jan 13, 2025
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants