I need some advice with regards to how to code something. I am using CURL to retrieve the html on a page and on that page is a <select> field. I would like PHP to extract the highest value from the select box, please take below as an example:
er… be careful doing that, Guido - if there’s more than one select box on the page (Like… a language dropdown?), that could end up giving some very bad responses.
Lets make sure we get the -specific- box we’re after.
Something a bit more like…
I would advise against using Regex for matching html attributes as that leaves you prone to many errors. As suggested use a DOM parser witch php does have many bolted on.
See this as a ++ to jgetner’s suggestion of using a parser to parse. Lives will be saved. Hair will remain on head. Orphan children will simply grow old without fulfilling prophesies of wizardry, and instead will marry overweight suburbanites and work in insurance until they retire.
Though querypath reminds me of Python’s libxml, also sounds good.
The first thing I’d do, since the element has a proper ID attribute, is use simple string methods to extract that <select> element from the source. strpos to find the start position of that particular <select>, strpos to find the position of the <select> element’s closing tag, and substr to extract it.
Then I’d pass the extracted string to one of the DOM libraries mentioned.
Thanks for your answers guys but one final question:
How can I extract an iframe from HTML? I.e. I just want to return the src of the iframe, lets take the following example:
<iframe title=“paymentServicesiframe” id=“paymentServicesiframe” src =“https://ips.ihost.com/hpp/checkout.hpp?sessionId=ADWSGET716SJWY2” frameborder=“0” align=“middle” scrolling=“no” height=“460px” width=“709px”> Your browser does not support in-line frames or is currently configured not to display in-line frames. </iframe>