From select box to array
I have a series of select boxes that I'd like to get data from, essentially turning them into an array. What's the most 开发者_StackOverflow社区efficient way to do this? Right now I'm thinking....
$html = file_get_contents('http://www.domain.com');
preg_match_all("/name\=\'subscription\[division_id\]\' style\=\'width: 170px;\'>(.+?)<\/select>/is", $html, $matches);
Then I was thinking of running other code to take the option tags into an array, but this seems it might be a little unnecessarily intensive
If you are scraping for whatever reason, you could probably parse the page's html with php's DOMXPath commands. I can't write out all the code, but you can get started with:
$xpath = new DOMXPath($dom);
$select_values = $xpath->evaluate("/html/body//option");
Then you run everything through a loop getting the contents of the options. Anyway, with something like this you can avoid all the nonsense with regex.
精彩评论