简体   繁体   English

从选择框到阵列

[英]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. 如果由于某种原因而进行抓取,则可以使用php的DOMXPath命令来解析页面的html。 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. 无论如何,使用这样的东西,您可以避免使用正则表达式的所有废话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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