1

Any suggestion how to do the result value option without disabled option with php preg_match_all?

<option value="1" disabled="disabled"> google</option>
<option value="2" selected="selected">- yahoo</option>
<option value="3">- youtube</option>
<option value="4">- facebook</option>
<option value="5" disabled="disabled"> twitter</option>
<option value="6">- tumblr</option>
<option value="7">- bing</option>

how to echo just value without disabled options maybe with preg_match_all?

i have some code but not work,

   $string = array(
   '/<option value=\"(.*)\" selected=\"selected\">-/sU',
   '/<option value=\"(.*)\">-/U',);

$option = curl_exec($ch);
foreach ($string as $key) {
 if (preg_match_all($key, $option, $matches)) {
  $count = count($matches[1]);
    for ($i=0; $i < $count ; $i++) { 
     echo $matches[1][$i]."\n";
  }
 }else {
   echo "match NOT found";
 }
}

i want to the result like this,

value = 2,3,4,6,7

thanks for helping,

2 Answers2

1

Using XPath is easy, check the following solution:

$doc = new DOMDocument();
$doc->loadHTML('<option value="1" disabled="disabled"> google</option><option value="2" selected="selected">- yahoo</option><option value="3">- youtube</option><option value="4">- facebook</option><option value="5" disabled="disabled"> twitter</option><option value="6">- tumblr</option><option value="7">- bing</option>');

$xpath = new DOMXPath($doc);
$notDisabledOptions = $xpath->query("//option[not(@disabled)]");
$numberOfOptions = $notDisabledOptions->length;

for($i = 0; $i < $numberOfOptions; $i++){
      echo $notDisabledOptions->item($i)->attributes->getNamedItem("value")->nodeValue;
}

For more info on how to use XPath, check this XPath tutorial.

Boy
  • 1,182
  • 2
  • 11
  • 28
  • thanks Boma, this code work fine, the result is value select (google, yahoo, etc) but i want to the result is value="xx" (the xx) so the result like this: 2,3,4,6,7 not yahoo- youtube- facebook- tumblr- bing – jose ramirez Apr 17 '16 at 12:29
  • @jose Check the code now, It returns attribute value. – Boy Apr 17 '16 at 12:42
  • thanks for fast reply, I got error with attribute value: Fatal error: Cannot use object of type DOMNamedNodeMap as array on line 12 – jose ramirez Apr 17 '16 at 12:45
  • It's working fine here, i guess you have older version of php, try now, this should work – Boy Apr 17 '16 at 12:53
  • wow, thanks Borna, now its work fine, thanks for the help Borna – jose ramirez Apr 17 '16 at 12:56
  • no problem :) just vote as accepted answer please. :) – Boy Apr 17 '16 at 12:57
0

Well, you can set the option values in the array. And then manipulate that array according to your likings.

Check this answer out, this might be of the same answer to your question.

How to get multiple selected values of select box in php?

You put your < options > in a < select > tag, and then identify a name < select name="name" > and retrieve your selected options by referencing the select name tag.

Please let me know if it helped, or didnt.

Community
  • 1
  • 1
MakDo
  • 86
  • 10