Regular expression?
Moderator: General Moderators
Regular expression?
Dear All
http://uk.finance.yahoo.com/m5?a=1&s=GBP&t=EUR
I really need a regular expression to enable me to 'fish out' the value under 'Euro', where the table goes
Symbol - British Pound - Exchange Rate - Euro <-- I need this
Can anyone tell me what this regular expression would be (to find from opening the HTML of the page via fopen)
Many thanks
Mark
http://uk.finance.yahoo.com/m5?a=1&s=GBP&t=EUR
I really need a regular expression to enable me to 'fish out' the value under 'Euro', where the table goes
Symbol - British Pound - Exchange Rate - Euro <-- I need this
Can anyone tell me what this regular expression would be (to find from opening the HTML of the page via fopen)
Many thanks
Mark
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
not tested
Code: Select all
//once you got the returned html...
function getConversion($html,$symFrom = "GBP",$symTo = "EUR")
{
$innerHTML = preg_split("|\</?[^>]*?\>|",html_entity_decode($html,ENT_QUOTES));
$cnt = sizeof($innerHTML);
$searchStart = "{$symFrom}{$symTo}=X";
$startCounting = false;
$counter = 0;
$result = false;
for($x = 0; $x < $cnt; $x++)
{
$curElem = trim($innerHTML[$x]);
if(!startCounting && strcasecmp($curElem,$searchStart)===0)
{
$startCounting = true;
}
elseif($startCounting && !empty($curElem) && ++$counter == 4)
{
$result = (float)$curElem;
break;
}
}
return $result;
}Code: Select all
if (preg_match('/((?:\d+\.)??\d+)<\/b>$/m', $string, $match))
{
echo $match[1];
}No problem. The only thing I would be hesitant about is relying on third parties which are completely outwith your control for this type of info.
Not sure exactly what you are doing, but if it were me and it was important I would consider grabing this info once per day and combining that with the code sending me an email of the result. That way you could manually adjust things if for some reason you cannot connect to their site or the code returns the wrong/unexpected result.
Not sure exactly what you are doing, but if it were me and it was important I would consider grabing this info once per day and combining that with the code sending me an email of the result. That way you could manually adjust things if for some reason you cannot connect to their site or the code returns the wrong/unexpected result.