I have tried to obtain the exchange rate of the dollar to my local currency, since I do not have an API at my disposal, I would like to scrape a website of a local bank that shows this data.
Thanks to the recommendation of one of the answers to this question in English, I have found the Simple HTML DOM library . Following the documentation, it is a fairly simple library to use.
Problem:
The website I want to get the data from is a fairly simple Source Site ; the only detail is that the information is inside a single one DIV
and I would like to obtain the Purchase and Sale values separately, to store each of the values in a variable, is this possible?
...<div class="conteTasa">Compra: <b>24.0199</b> <br>Venta: <b>24.1880</b></div>...
What I have tried:
<?php
include('simple_html_dom.php');
$htmlsite = file_get_html('https://www.banpais.hn/barradolar.php');
$element = $htmlsite->find('div[class="conteTasa"]',0);
echo $element;
?>
Buy: 24.0199 Sell: 24.1880
But as you can see, I get the entire div as a response.
I would appreciate any comment
Greetings.
If
$element
it contains this:You can use strip_tags() to remove the HTML tags, remove the words and leave only the amounts with str_replace() and finally detach with explode()
Of course, some validation would still be missing, like checking that it
$importes
has two elements ( count() ) and that they are both numeric ( floatval() )I don't know if there is a better way, but you can use php's explode function to create an array. for instance
The result
Then we store in variables the value that returns
I use str_replace to remove the values I don't need
Result
We clean the remaining labels
Result