开发者

From select box to array

开发者 https://www.devze.com 2023-02-07 07:00 出处:网络
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....

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号