Regular expression?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Regular expression?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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;
}
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

Thanks feyd, I'll give it a go
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post 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
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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).
mjseaden
Forum Contributor
Posts: 458
Joined: Wed Mar 17, 2004 5:49 am

Post by mjseaden »

That's great! Thank you very much!
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
Post Reply