Jump to content

Get content of a website and save a section of that content to a variable


JohnS

Recommended Posts

Hi,

 

I will start off trying to explain what I am trying to make the best I can. What I want to create is a script that gets the gold value from this website: http://www.lbma.org.uk/pages/index.cfm?page_id=46&title=current_statistics and then save it to a variable which I will use to calculate values of different gold karats.

 

Here is the content in bold I need on the website I linked:

LONDON GOLD FIXING

          USD           GBP           EUR

AM 1588.00    1005.127   1251.083

PM 1589.50    1004.741    1249.803

 

 

So what help do I need? Well, I don't expect you to figure out the calculating part for me but some help how to get that content pointed out above and save it to a variable is what I would appreciate getting some help with.

 

I don't know much PHP, only some and I have been trying to figure this out for a day now without any success.  I suppose php get contents and/or curl should be used here but I don't know how really. I would very much appreciate the help I can get on this.

 

Thank you!

Link to comment
Share on other sites

Hi JohnS!

 

Take a look at Simple HTML DOM parser. Basically you can get the gold value by using:

$html = file_get_html('http://www.lbma.org.uk/pages/index.cfm?page_id=46&title=current_statistics');
$element = $html->find('#pricing_goldCurrent table tbody tr td')->children(1);
$goldValue = (float)$element->plaintext;

 

If that doesn't work try out descending from the div id 'pricing_goldCurrent'. Simple HTML DOM also has an adequate documentation to get you well on your way.

Link to comment
Share on other sites

Hey,

 

Thanks for your answer. I've been trying to use your code for quite some time now and also others Simple HTML DOM parser code but most of it returns me a page which says server error. Tried many different codes but nothing is working. I have put the simple_html_dom.php file in the root folder together with the index.php file, doesn't work though. Got any idea how to resolve this?

 

Thanks

Link to comment
Share on other sites

preg_match('/RegEx for String/', $subject, $matches);

var_dump($matches);

 

Any values matching the RegEx (which could just be a sentence) will populate the $matches variable as array values.

Link to comment
Share on other sites

What would the input in RegEx be if I need the content of #pricing_goldCurrent be? I found: #<div\b[^>]*class=([\'"])?votes(?(1)[\'"])[^>]*>.*?</div>#s however it doesn't work translate here to my div id. Thanks.

Link to comment
Share on other sites

Hey,

 

Thanks for your answer. I've been trying to use your code for quite some time now and also others Simple HTML DOM parser code but most of it returns me a page which says server error. Tried many different codes but nothing is working. I have put the simple_html_dom.php file in the root folder together with the index.php file, doesn't work though. Got any idea how to resolve this?

 

Thanks

 

Update to my previous post: If you're still looking for the bolded price, the following code actually gets you the results:

$goldPrice = (float)$html->find('#pricing_goldCurrent table tbody tr td', 1)->plaintext;

 

And what comes to the server error:

I was working with simple html dom with a server that had file_get_contents() disabled, so i had to replace the content loading mechanism. Obviously this is not the case since you can run regex on the content. The library is pain in the ass in a sense that it fills up the memory freakishly easily and as a result you just get a boring 500 internal server error. And the best solution i came up with is to debug step by step the freaking code. Fortunately clear() function frees up all unused stuff real nicely. The previous code i supplied caused some sort of loop and raised an error since it didn't return an object. This might be one of the reasons for server error.

 

If you decide to stick with the library, try to var_dump the $html you loaded and see do you get anything out of it. If you do the code above will get the price for you.

 

And yeah like CPD mentioned regex works too, but it tends to complicate things more than necessary. I was trying to make a case against regex and it's extensibility in this context but couldn't come up with anything. Fair enough.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.