php , simple_html_dom.php, 获取所选选项
我有一个像这样的html块:
$localurl = '
<select name="cCountry" id="cCountry" style="width:200" tabindex="5">
<option value="251">Ascension Island</option>
<option selected="selected" value="14">Australia</option>
<option value="13">Austria</option>
';
我正在尝试使用 simple_html_dom ( http: //simplehtmldom.sourceforge.net/ )。到目前为止,我已经构建了一个函数,但不起作用:
//extract the selected value
function getValue_selected($value, $localurl) { $html = file_get_html($localurl); $i = 0; foreach ($html->find('select[option selected="selected"]') as $k => $v) { if ($v->name == $value) { $shows[$i]['Location'] = $v->value; } } $value = $shows[$i]['Location']; $html->clear(); unset($html); return $value; } $selected_value = getValue_selected('cCountry', $localurl)
类似 QueryPath 的替代方案也将被接受。
I have a html block like this :
$localurl = '
<select name="cCountry" id="cCountry" style="width:200" tabindex="5">
<option value="251">Ascension Island</option>
<option selected="selected" value="14">Australia</option>
<option value="13">Austria</option>
';
I'm trying to extract the selected value in this case "Australia" using simple_html_dom ( http://simplehtmldom.sourceforge.net/ ). So far I have build a function but is not working :
//extract the selected value
function getValue_selected($value, $localurl) { $html = file_get_html($localurl); $i = 0; foreach ($html->find('select[option selected="selected"]') as $k => $v) { if ($v->name == $value) { $shows[$i]['Location'] = $v->value; } } $value = $shows[$i]['Location']; $html->clear(); unset($html); return $value; } $selected_value = getValue_selected('cCountry', $localurl)
An alternative such QueryPath would be accepted too .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正确答案是:
the right answer is:
我的猜测是,当
$shows
在函数外部定义时,您正在尝试访问它。如果这是问题所在,您需要将global $shows;
放在 func 的顶部,或者更好的是,修改签名以将其传入。例如:My guess is that you're trying to access
$shows
when it is defined outside of the function. If this is the problem, you either need to putglobal $shows;
at the top of the func, or, better still, modify the signature to pass it in. Something like: