PHPQuery从下拉列表中选择所有值

Sha*_*aul 11 php phpquery

我需要根据id使用PHPQuery的下拉列表获取数组中下拉列表的所有值.

以下是HTML:

<select name="semester" id="semester" class="inputtxt" onChange="javascript:selectSemester(this, this.form);">
    <option value="">-- Select your Semester --</option>
    <option value="2nd" selected>2nd</option>
    <option value="4th" >4th</option>
    <option value="6th" >6th</option>
    <option value="8th" >8th</option>
    <option value="SE1" >SE1</option>
    <option value="SE3" >SE3</option>
    <option value="SE5" >SE5</option>
    <option value="SE7" >SE7</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我试过这个:

$semesters = $all['#semester'];

foreach ($semesters as $semester) {
    echo pq($semester)->text();
    echo '<br>';
}
Run Code Online (Sandbox Code Playgroud)

但是我只得到一个输出,并且所有值都连接在一起.如何将每个值作为数组中的单独元素?

Ser*_*hik 4

这段代码对我来说效果很好:

// include part...

$ids = array();

$raw = file_get_contents("http://localhost:8000/test.html"); // your url

$doc = phpQuery::newDocument($raw);

phpQuery::selectDocument($doc);

/** @var DOMElement $opt */
foreach (pq('#semester > option') as $opt) {
    $ids[] = ($opt->getAttribute('value'));
}

print_r($ids); // check if the array has the values stored
Run Code Online (Sandbox Code Playgroud)

所以结果是

Array
(
    [0] => 
    [1] => 2nd
    [2] => 4th
    [3] => 6th
    [4] => 8th
    [5] => SE1
    [6] => SE3
    [7] => SE5
    [8] => SE7
)
Run Code Online (Sandbox Code Playgroud)

顺便说一句,您可以使用$doc['#semester > option']代替pq('#semester > option'),两种变体都可以正常工作。如果您需要省略一些option- 您可以根据option属性进行过滤,例如if ($opt->getAttribute('value') != "").