C
C
C4H9I2019-09-03 11:09:25
PHP
C4H9I, 2019-09-03 11:09:25

Simple html dom parser - sequence of elements on a page?

Hello,
the question is probably simple, but something does not work for me. The task is to parse the page and select H2 and H3 tags from it, there is no problem with this. But you need to output them in such a way that the sequence is preserved, that is, if the text looks like this:

<h2>Заголовок 1</h2>
....
<h3>Заголовок 1.1</h3>
...
<h3>Заголовок 1.2</h3>
...
<h2>Заголовок 2</h2>
...
<h3>Заголовок 2.1</h3>
...
<h2>Заголовок 3</h2>
...
<h3>Заголовок 3.1</h3>
...

Between the headings, of course, text, pictures, etc.
How to output data with headings in the sequence of their location on the page:
(Heading 1, Heading 1.1, Heading 1.2, Heading 2, Heading 2.1)
I did several searches for h2 and h3 tags and got a sequence of h2 tags and h3 tags not related by order :
foreach($html->find('h2') as $h2Tag){
$htags['h2'][]=$h2Tag->plaintext;
}
foreach($html->find('h3') as $h3Tag){
$htags['h3'][]=$h3Tag->plaintext;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-09-03
@C4H9I

$html->find('h2, h3') won't give an array with both tags together?
https://simplehtmldom.sourceforge.io/manual.htm#se...

// Find all anchors and images with the "title" attribute
$ret = $html->find('a[title], img[title]');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question