JohnS Posted May 19, 2012 Share Posted May 19, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/ Share on other sites More sharing options...
noXstyle Posted May 19, 2012 Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346795 Share on other sites More sharing options...
JohnS Posted May 19, 2012 Author Share Posted May 19, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346802 Share on other sites More sharing options...
cpd Posted May 19, 2012 Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346808 Share on other sites More sharing options...
JohnS Posted May 19, 2012 Author Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346823 Share on other sites More sharing options...
cpd Posted May 19, 2012 Share Posted May 19, 2012 What format are you expecting it to come in, an example would be ideal. Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346833 Share on other sites More sharing options...
noXstyle Posted May 19, 2012 Share Posted May 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1346886 Share on other sites More sharing options...
JohnS Posted May 20, 2012 Author Share Posted May 20, 2012 Continues getting same the same error. You can see it here: www.stugsajt.se Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1347066 Share on other sites More sharing options...
JohnS Posted May 20, 2012 Author Share Posted May 20, 2012 Continues getting same the same error. You can see it here: www.stugsajt.se Thanks Quote Link to comment https://forums.phpfreaks.com/topic/262770-get-content-of-a-website-and-save-a-section-of-that-content-to-a-variable/#findComment-1347067 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.