I have one string in PHP that forms HTML select box. I am also having one array. What I want is: I want to compare options of the select box with array elements and only keep the options in select box that match with array elements. Following is the code that will make the concept more clear.
String forming select box is:
$str = '<select name="options[45]" id="select_45" class=" required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
<option value="" >-- Please Select --</option>
<option value="76" price="0" >Certified Networking </option>
<option value="89" price="0" >Certified Virtualization </option>
<option value="90" price="0" >Certified Expert Virtualization </option><option value="91" price="0" >Certified Mobility </option>
<option value="92" price="0" >Certified Professional Networking </option><option value="93" price="0" >Certified Professional Virtualization </option>
</select>';
Array is:
$array = array('-- Please Select --','Certified Networking', 'Certified Mobility');
After comparison, I want string as:
$str = '<select name="options[45]" id="select_45" class=" required-entry product-custom-option" title="" onchange="opConfig.reloadPrice()">
<option value="" >-- Please Select --</option>
<option value="76" price="0" >Certified Networking </option>
<option value="91" price="0" >Certified Mobility </option>
</select>';
I have tried using DOM This is giving me the option text from string. But I am stuck at this and can't find the way to move further.
$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$options = $xpath->query('//select/option');
foreach ($options as $option) {
echo $option->nodeValue;
}
strip_tags()valueandpricecontaining dynamic values. So cant create new string from array.. :-(