久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

PHP-比較字符串部分與數(shù)組值

 印度阿三17 2019-11-19

我有一個(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

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn),。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買(mǎi)等信息,,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多