3

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;
}
3
  • php.net/manual/en/function.strip-tags.php strip_tags() Commented Oct 14, 2015 at 5:03
  • why don't yo create new string by array ?? Commented Oct 14, 2015 at 5:33
  • The string is generating dynamically with value and price containing dynamic values. So cant create new string from array.. :-( Commented Oct 14, 2015 at 5:41

4 Answers 4

2

you can use regular expression's power like below. but remember it only works for your type, this means only work if you have an select and options tag.

$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 = array('-- Please Select --','Certified Networking', 'Certified Mobility');

foreach ($array as $index=>$value) {
    $array[$index] = preg_replace(array('/([\-\+\*\$\^\{\}\[\]\<\>\.\?\\/\\\\])/', '/\s+/'), array('\\\\$1', '\\\\s+'), $value);
}
$str = explode("\n", str_replace('</option><option', "</option>\n<option", $str));
$regexp = '/^(?:\s*(?:\<\/?select[^\>]*\>[^\n]*|\<option[^\>]*\>\s*(?:'.implode('|', $array).')\s*\<[^\n]*)\s*)$/i';
foreach($str as $index=>$value) {
    if (!preg_match($regexp, $value, $match)) {
        unset($str[$index]);
    }
}
$str = implode("\n",$str);

Edit In case you have any html that contains your select option case, use the one edited below:

$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 = array('-- Please Select --', 'Certified Networking', 'Certified Mobility');

foreach ($array as $index => $value) {
    $array[$index] = preg_replace(
        array('/([\-\+\*\$\^\{\}\[\]\<\>\.\?\\/\\\\])/', '/\s+/'),
        array('\\\\$1', '\\\\s+'),
        $value
    );
}
$str = explode("\n", str_replace('</option><option', "</option>\n<option", $str));
$regexp = '/^(?:\s*(?:\<option[^\>]*\>\s*(?:' . implode('|', $array) . ')\s*\<[^\n]*)\s*)$/i';
foreach ($str as $index => $value) {
    if (stripos($value, '<option') !== false && !preg_match($regexp, $value, $match)) {
        unset($str[$index]);
    }
}
$str = implode("\n", $str);
Sign up to request clarification or add additional context in comments.

Comments

1

This may help you , It does not need any kind of RegExp.

 $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         = array('-- Please Select --','Certified Networking', 'Certified  Mobility');
 $strToArray = explode("</option>",$str);
 foreach ($strToArray as $stringOption) {
      foreach ($array as $string) {
         if (strpos($stringOption,$string)) {
            echo $stringOption ;
         }
      }
 }

Comments

0

The main problem is in the original string you have spaces after the names in your options so it was not reading them in the in_array() function.

Then it is just a simple if statement to filter the options you want then add them back to a string

    $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>';

$origanalDom = new DOMDocument();
$origanalDom->loadHTML($str);
$xpath = new DOMXPath($origanalDom);

$options = $xpath->query('//select/option');

$array = array(
    '-- Please Select --',
    'Certified Networking',
    'Certified Mobility'
);

// add opening select tag to a string
$newString = '<select name="options[45]" id="select_45" class=" required-entry product-custom-option" title=""  onchange="opConfig.reloadPrice()">';

foreach ($options as $option) {
    if (in_array($option->nodeValue, $array)) {

        // add option tag to the string if text is in array

        $newdoc = new DOMDocument();
        $cloned = $option->cloneNode(true);
        $newdoc->appendChild($newdoc->importNode($cloned, true));
        $newString .= $newdoc->saveHTML();
    }
}

// close the select tag
$newString .= '</select>';

echo $newString;

2 Comments

One more addition will have to be done is $newString = '<select name... also needed to extract from $str as opening select tag is dynamic too. Rest of the approach is fine. Thanks @Adrian
-1 because your solution is not smart and is highly dependant on the html input. The actual smart solution would be using regular expression or manipulating strings not changing the html.
0

There can be better answers, this is a workaround kind of solution.

<?php 
 $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 = array('-- Please Select --','Certified Networking', 'Certified Mobility');
$out = substr($str,0, strpos($str,"<option"));
$sub =  substr($str, strpos($str,"<option"),strpos($str,"</select")-strpos($str,"<option"));
$ar = explode("</option><option",$sub);
$size = sizeof($ar);
$ar[0] = str_replace("<option","",$ar[0]);
$ar[$size-1] = str_replace("</option>","",$ar[$size-1]);
for ($i=0;$i<sizeof($array);$i++)
{
    for($j=0;$j<$size;$j++)
    {
        if(strpos($ar[$j],$array[$i])>0)
        {
        $out.='<option '.$array[$i].'</option>';
        }
    }
}
$out .="</select>";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.