Chuyển HTML select option sang array PHP

19 Th7 2021
0 Comments
                
                    
<?php
$s = '<select id="province_name" name="province_name">
<option value="">Chọn đài</option>
<option value="binh-dinh">Bình Định</option>
<option value="da-nang">Đà Nẵng</option>
<option value="dac-lac">Đắc Lắc</option>
<option value="quang-ngai">Quảng Ngãi</option>
<option value="gia-lai">Gia Lai</option>
<option value="khanh-hoa">Khánh Hòa</option>
<option value="kon-tum">Kon Tum</option>
<option value="ninh-thuan">Ninh Thuận</option>
<option value="thua-thien-hue">Thừa Thiên Huế</option>
<option value="quang-tri">Quảng Trị</option>
<option value="dac-nong">Đắc Nông</option>
<option value="quang-nam">Quảng Nam</option>
<option value="quang-binh">Quảng Bình</option>
<option value="phu-yen">Phú Yên</option>
</select>';
    $matches = null;
    $result = array();
    $final = array();

    if(preg_match_all('/value="(.*)".*?>(.*)<\\/option>/', $s, $result)){
        $matches = array_pop($matches);
        foreach($result[1] as $i => $key){
            $key = html_entity_decode($key);
            $val = html_entity_decode($result[2][$i]);
            $final[$result[1][$i]] = $val;
        }
    }

    var_export($final);
                
            

Trả lời

Lọc
Wishlist
Account
FB Chat
This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.