Page 1 of 1
Regular expression?
Posted: Sat May 15, 2004 4:48 am
by mjseaden
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
Posted: Sat May 15, 2004 5:42 am
by feyd
not testedCode: 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;
}
Posted: Sat May 15, 2004 5:59 am
by mjseaden
Thanks feyd, I'll give it a go
Posted: Sat May 15, 2004 7:48 am
by mjseaden
What is html_entity_decode? I don't seem to have this in my PHP version, and it's a pretty new version.
Posted: Sat May 15, 2004 9:56 am
by tim
i believe it was added in php version 4.3.0 and php5
it converts strings, notice the ENT_QUOTES
that will convert both single/double quotes
you should have it.
read the manual for more info.
Posted: Wed May 19, 2004 9:15 am
by mjseaden
Hi! I've tested the code and it doesn't seem to work. I've had to remove the html_entity_decode because my PHP version reports that it doesn't recognise it. I'd really appreciate any advice on this, as it is a pretty important function in my site.
Thanks very much
Mark
Posted: Wed May 19, 2004 9:40 am
by redmonkey
Code: Select all
if (preg_match('/((?:\d+\.)??\d+)<\/b>$/m', $string, $match))
{
echo $match[1];
}
Assumes that $string is the contents of the page, and the page is always formatted the same (which it should be).
Posted: Wed May 19, 2004 11:50 am
by mjseaden
That's great! Thank you very much!
Posted: Wed May 19, 2004 3:07 pm
by redmonkey
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.