php , simple_html_dom.php, get selected option

巧了我就是萌 提交于 2019-12-02 19:13:15

问题


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 .


回答1:


the right answer is:

$html->find('#cCountry',0)->find('option[selected=selected]',0);



回答2:


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 put global $shows; at the top of the func, or, better still, modify the signature to pass it in. Something like:

getValue_selected($value, $localurl, &$shows)
{/* your function here */ }

getValue_selected($val1, $val2, $shows);


来源:https://stackoverflow.com/questions/6484103/php-simple-html-dom-php-get-selected-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!