Search code examples
phpsimple-html-dom

PHP Simple HTML DOM Parser the element just after current element


I have this HTML:

<div class="card">
    <span class="label">Name:</span><span>John Doe</span>
    <span class="label">Address:</span><span>Some address...</span>
    <span class="label">Email:</span><span>[email protected]</span>
    <span class="label">Tel:</span><span>xxxxxxxxx</span>
</div>



$spans = $html->find('.label');
foreach ($spans as $span) {
    if ($span->plaintext == 'Name:') $name = John Doe;
    (ALWAYS THE SPAN NEXT TO THE CURRENT .label SPAN)
}

Now how to get the value (plaintext) of the span NEXT to the the current span.

NB: The order of the labels and their values is continuously changing, sometimes the TEL before ADDRESS...


Solution

  • You are looking for nextSibling() http://simplehtmldom.sourceforge.net/manual_api.htm

    if ($span->plaintext == 'Name:') {
        $name = $span->nextSibling()->plaintext;
    }