我有一個(gè)字符串在PHP中形成HTML選擇框.我也有一個(gè)數(shù)組.我想要的是:我想將選擇框的選項(xiàng)與數(shù)組元素進(jìn)行比較,而只將與數(shù)組元素匹配的選項(xiàng)保留在選擇框中. 以下是使概念更清晰的代碼.
字符串形成選擇框?yàn)椋?/p>
$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>';
數(shù)組是:
$array = array('-- Please Select --','Certified Networking', 'Certified Mobility');
經(jīng)過(guò)比較,我想要字符串為:
$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,這使我可以從字符串中獲取選項(xiàng)文本.但是我一直堅(jiān)持下去,找不到進(jìn)一步的方法.
$dom = new DOMDocument();
$dom->loadHTML($str);
$xpath = new DOMXPath($dom);
$options = $xpath->query('//select/option');
foreach ($options as $option) {
echo $option->nodeValue;
}
解決方法: 您可以使用正則表達(dá)式的功能,如下所示.但請(qǐng)記住,它僅適用于您的類(lèi)型,這意味著僅當(dāng)您具有select和options標(biāo)記時(shí)才有效.
$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);
編輯 如果您有任何包含選擇選項(xiàng)大小寫(xiě)的html,請(qǐng)使用下面編輯的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);
來(lái)源:https://www./content-1-565901.html
|