简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM