D
D
drunken_ikarus2020-01-31 16:55:07
Parsing
drunken_ikarus, 2020-01-31 16:55:07

Errors when parsing DOMDocument, what is the reason?

There is an array like this:

$arr = array(
0 => array( 'snapshot' => '<div xmlns="http://www.w3.org/1999/xhtml" id="pages-content"><div class="image"><img src="/2019/12/1.jpg" /></div><div class="image"><img src="/2019/12/11.jpg" /></div></div>'),
1 => array( 'snapshot' => '<div xmlns="http://www.w3.org/1999/xhtml" id="pages-content"><div class="image"><img src="/2019/12/2.jpg" /></div><div class="image"><img src="/2019/12/22.jpg" /></div></div>'),
)


there is a function:
public function getPhotobookImages($snapshot) {
$result = array();
    $dom = new DOMDocument;
    $dom->loadHTML($snapshot);
    $images = $dom->getElementsByTagName('img');
    foreach ($images as $image) {
      $result[] = $image->getAttribute('src');
    }

    return $result;
  }

I want to get the src attribute of all img tags, I iterate through the array:
foreach($arr as $a) {
$images[] = $this->getPhotobookImages($a['snapshot'])
}

I end up with errors:
Warning: DOMDocument::loadHTML(): ID nwgrip already defined in Entity, line: 1 in (строка где: $dom->loadHTML($snapshot);)
Warning: DOMDocument::loadHTML(): ID negrip already defined in Entity, line: 1 in (строка где: $dom->loadHTML($snapshot);)
Warning: DOMDocument::loadHTML(): ID nwgrip already defined in Entity, line: 1 in (строка где: $dom->loadHTML($snapshot);)
Warning: DOMDocument::loadHTML(): ID negrip already defined in Entity, line: 1 in (строка где: $dom->loadHTML($snapshot);)

If you send only one value from the $arr array to getPhotobookImages, then everything works, there are no errors

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kikimarik, 2020-01-31
@kikimarik

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML(mb_convert_encoding($snapshot, 'HTML-ENTITIES', 'UTF-8'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question