0

I structure like this:

<tr>
    <td>
        <strong>Tel. nr.:</strong>
        +370 000 000
        <strong>Faksas:</strong>
        +370 5 0000
    </td>
</tr>

I new in using Simple HTML DOM. What I need, I need content +370 000 000 and +370 5 0000 . I see that this library does not support Xpath, how can I write a query where I can extract the contents after the HTML link <strong>Tel. nr.:</strong> ?

I found only one way, get HTML and with regex get text from </strong> till <strong>, but maybe Simple HTML DOM have own method for this?

2
  • 1
    where are your codes? try to use children inside the <td> Commented Jan 17, 2015 at 15:48
  • I think the RegEx method you provided is legit. I just went through the documentation, and you can simplify your work by examining all data from </strong> to < instead of </strong> to <strong> Commented Jan 17, 2015 at 15:54

2 Answers 2

1

Try like this...

   <?php
    require('simple_parser.php');
    $html = str_get_html('
    <tr>
        <td>
            <strong>Tel. nr.:</strong>
            +370 000 000
            <strong>Faksas:</strong>
            +370 5 0000
        </td>
    </tr>');
    $td =$html->find('td',0) ; 
    echo $td->plaintext;

    ?>

Post your full code to get a clear answer

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

Comments

1

You could use ->find('text') in order to get the text nodes:

$sample_html = '
<table>
<tr>
    <td>
        <strong>Tel. nr.:</strong>
        +370 000 000
        <strong>Faksas:</strong>
        +370 5 0000
    </td>
</tr>
</table>
';

$html = str_get_html($sample_html);
foreach($html->find('tr') as $row) {
    $first_td = $row->find('td', 0);
    echo $first_td->find('text', 2);
    echo $first_td->find('text', 4);
}

But this solution is rather clunky. One removal of those newlines on the elements would yield another result.

I suggest use DOMDocument with xpath instead:

$dom = new DOMDocument;
$dom->loadHTML($sample_html);
$xpath = new DOMXpath($dom);
$elements = $xpath->query('//tr[1]/td[1]/text()');
foreach($elements as $e) {
    echo trim($e->textContent) . '<br/>';
}

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.