1

I try Simple HTML DOM Parser to get specific text from external website. Here is my code:

include('simple_html_dom.php');
    $html = file_get_html('http://www.bca.co.id/id/biaya-limit/kurs_counter_bca/kurs_counter_bca_landing.jsp');

foreach ( $html->find('td',13) as $raw) {
    echo $raw->plaintext;
}

So far, I get one line result:

HKD 1502.30 1470.40

HKD is the currency. 1502.30 is the sell rate and 1470.40 is the buy rate.

How to get 1502.30 value only and use it in another PHP files? I want this value can be used to create automatic currency rate conversion. Thanks.

3 Answers 3

3

Find the <tr> block first, loop through the children and retrieve the item you want:

foreach ($html->find('tr',5)->children() as $raw) {
    $values[] = $raw->plaintext;   
}
echo $values[1];

Output:

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

1 Comment

Thank you, it works with Simple HTML DOM Parser. I got a lesson here :)
0

Couldn't you just use explode? (assuming you always get this like output with spaces)

$value = explode(" ",$raw->plaintext);
echo $value[1]; //your desired value

1 Comment

probably work in some case but it doesn't work because I choose wrong tag selector. thanks.
0

The original HTML is this:

<tr>
    <td style="text-align:center;">HKD</td>
    <td style="text-align:right;">1502.30</td>
    <td style="text-align:right;">1470.40</td>
    <!-- kolom dua -->
</tr>

... so all three values are already separated. You're apparently just not storing them and, instead, simply printing them:

foreach ( $html->find('td',13) as $raw) {
    echo $raw->plaintext;
}

I've never used that library but it makes more sense to find <tr>'s first. Then, there's no problem matching the related values:

// Completely untested (I even made up the function names)
foreach ($html->find('tr') as $tr) {
    $cells = $tr->find('td');
    $currency = $cells[0]->getValue();
    $sell = $cells[1]->getValue();
    $buy = $cells[2]->getValue();
}

1 Comment

This explain everything completely. Thank you.

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.