Jump to content

Get some texts from the content of a page


kleidi24

Recommended Posts

Hello,

   I was looking around for a solution to get some texts from a webpage. I was searching around for some solutions but i can't make it works. I have a national bank webpage that every day shows the official exchange rates. I want to get thos official rates once a day and cache it. The bank page shows this html code:

[...]
<tr>
<td width="13" height="15" align=right>
<img src="bullet_light_grey.gif">
</td>
<td width="3">
<img src="spacer.gif" width="3" height="18">
</td>
<td class="arial11Light">USD / ABC</td>
<td class="arial11Light" align=right>
103.51
</td>
</tr>
<tr>
<td width="13" height="15" align=right>
<img src="bullet_light_grey.gif">
</td>
<td width="3">
<img src="spacer.gif" width="3" height="18">
</td>
<td class="arial11Light">EUR / ABC</td>
<td class="arial11Light" align=right>
140.26
</td>
</tr>
<tr>
<td width="13" height="15" align=right>
<img src="bullet_light_grey.gif">
</td>
<td width="3">
<img src="spacer.gif" width="3" height="18">
</td>
<td class="arial11Light">GBP / ABC</td>
<td class="arial11Light" align=right>
169.65
</td>
</tr>
[...]

This is the code where is found my needed informations. Now, what i need is the currency and the value. Ex: USD / ABC  -  103.51

Can you help me to find a way to extract this informations?

Thank you!

if they're always in the same order, you could use preg_match_all() to grab the values in order.

 

Assuming you've already grabbed the content and put it in a variable named $content.

preg_match_all('/([0-9]{2,3}\.[0-9]{2})/',$content,$matches);
print_r($matches);

would return:

Array
(
    [0] => Array
        (
            [0] => 103.51
            [1] => 140.26
            [2] => 169.65
            [3] => 113.88
        )
    [1] => Array
        (
            [0] => 103.51
            [1] => 140.26
            [2] => 169.65
            [3] => 113.88
        )
)

Hello there.

Thank you for your reply and for helping me. Those are always in the same order but the problem is that, are not the only numbers in the page :( and with way you thought, the return array may catch other numbers there. Or i'm wrong?

Thanks!

The canonical way to do this is with DOMDocument; however, you should probably check with the bank (or at least search the bank's site) to see if this is even permitted by the bank's Terms of Service.

 

You may have to call or write to them and ask if an API exists. Alternately, there are quite a few sites that can offer this sort of service, I think. For example, Google probably has an API (they certainly have the app online themselves).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.