比较字符串部分和数组值

时间:2015-10-14 04:59:20

标签: php arrays regex string dom

我在PHP中有一个形成HTML选择框的字符串。我也有一个阵列。我想要的是:我想比较选择框与数组元素的选项,只保留选择框中与数组元素匹配的选项。 以下是使概念更清晰的代码。

字符串形成选择框是:

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

比较后,我希望字符串为:

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

我尝试过使用 DOM 这是从字符串中提供选项文本。但我对此感到困惑,无法找到进一步发展的方法。

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

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

foreach ($options as $option) {
    echo $option->nodeValue;
}

4 个答案:

答案 0 :(得分:2)

你可以使用正则表达式的强大功能,如下所示。但请记住它只适用于您的类型,这意味着只有选择和选项标记才有效。

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

修改 如果您有任何包含select option案例的html,请使用下面编辑的

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

答案 1 :(得分:1)

这可能对您有所帮助,它不需要任何类型的RegExp。

Create table myTable(
...
PRIMARY KEY (ID, VALUE)
)

答案 2 :(得分:0)

主要问题是在原始字符串中,您在选项中的名称后面有空格,因此它没有在in_array()函数中读取它们。

然后它只是一个简单的if语句来过滤你想要的选项然后将它们添加回字符串

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

答案 3 :(得分:0)

可以有更好的答案,这是一种解决方法。

<?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>";
?>