1

Can I fetch the data between two html comments using Simple HTML Dom ??

For example, See the below code:

<!-- start of comment -->

<a href="link1.html" target="_blank">link1</a><br />
<a href="link2.html" target="_blank">link2</a><br />
<a href="link3.html" target="_blank">link3</a><br />
<a href="link4.html" target="_blank">link4</a><br />


<!-- end of comment-->

<a href="link5.html" target="_blank">link5</a><br />
<a href="link6.html" target="_blank">link6</a><br />

There are totally six links and only 4 links are enclosed within a "" and "" tags.

I just want to get the links between the comment tags.

0

2 Answers 2

1

You can do this:

//get all comments
$comments = $html->find('comment');

...and use next_sibling() to get next element and check if it's an anchor tag till you get another comment tag, where the script will terminate.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this code

$dom = new DOMDocument();
$dom->loadHTML($html);

$elements = $dom->getElementsByTagName('a');
foreach ($elements as $child) {
    echo $child->nodeValue;
}

1 Comment

This returns all anchors, not only the ones between comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.