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:
<select id="provide_vrm:prVRMfrag:prVRMCon:vrmRegistered" class="inputTextBox provideVrmWidth" size="1" name="provide_vrm:prVRMfrag:prVRMCon:vrmRegistered">
<option selected="selected" value="0">Select a vehicle</option>
How can I extract the highest option value? Ie. in this instance the value I want returned is 2 (value="2")
Please help! J
You can extract all values in an array with preg_match_all and a regular expression, something like
preg_match_all('%<option[.]* value="([^"]+)"%', $yourdata, $matches, PREG_PATTERN_ORDER);
where $yourdata contains the html code you got with curl.
Do a var_dump of $matches to see the result.
Then get the highest value from the array (take a look at rsort)
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...
preg_match_all('%<select id="provide_vrm:prVRMfrag:prVRMCon:vrmRegistered".*?(<option.*? value="([^"]+)">.+?</option>)+</select>%', $yourdata, $matches, PREG_PATTERN_ORDER);
(Note: This will change the location of your desired values in the $matches array, because we added another subpattern)
I know, I based my answer on the info in the OP
on that page is a <select> field
I believe there are DOM methods you could use in PHP to walk through the HTML hierarchy to get to the exact select box too.
I would recommend using query path which makes this and a whole lot more super simple when it comes to crawling strings of mark-up.
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.
Using regex to parse HTML? Oh my. This calls for some Zalgo.
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.
Yeah yeah, I got it...
Wow that is funny stuff. Talk about beating a dead horse :lol:
Can DOM be used to parse HTML that is not XHTML?
Based on the comments, I would say yes.
If a browser can do it, you can too. With all the mistakes browsers also make when the HTML is bad
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>
src is but an attribute of the iframe tag. You would grab it the same way you would grab any other element's attributes.
This topic is now closed. New replies are no longer allowed.