-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathExtractOptions.php
99 lines (94 loc) · 3.58 KB
/
ExtractOptions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
Purpose: Extract an array of values and display strings from dropdown select box html string
Author : Ap.Muthu <[email protected]>
Release: 2017-02-04
*/
function (extract_options($str, $pfx='" value="') {
$b = explode($pfx, $str);
$vals = array();
foreach ($b as $c) {
$d = explode('>',$c);
$d = explode('<',$d[1]);
$vals[$c+0] = $d[0];
// $vals[$c+0] = str_replace(' ','-',str_replace(',','',strtolower($d[0])));
}
$vals = array_filter($vals); // remove blank elements
ksort($vals);
return $vals;
}
/*
// Usage:
$a = <<<EOFL
<div class="blockcontent">
<label for="cat">Chhaisgarh Districts</label>
<select name="cat" id="cat">
<option value="-1">Select Category</option>
<option class="level-0" value="5">Ambikapur,Chhattisgarh</option>
<option class="level-0" value="26">Anjora,Chhattisgarh</option>
<option class="level-0" value="21">Bacheli,Chhattisgarh</option>
<option class="level-0" value="13">Baikunthpur,Chhattisgarh</option>
<option class="level-0" value="9">Bastar,Chhattisgarh</option>
<option class="level-0" value="16">Bhilai,Chhattisgarh</option>
<option class="level-0" value="6">Bilaspur,Chhattisgarh</option>
<option class="level-0" value="25">Chhattisgarh</option>
<option class="level-0" value="23">Chhattisgarh,Dantewada</option>
<option class="level-0" value="14">Chhattisgarh,Dhamtari</option>
<option class="level-0" value="7">Chhattisgarh,Durg</option>
<option class="level-0" value="4">Chhattisgarh,Jagdalpur</option>
<option class="level-0" value="29">Chhattisgarh,Janjgir-Champa</option>
<option class="level-0" value="15">Chhattisgarh,Jashpur</option>
<option class="level-0" value="11">Chhattisgarh,Kanker</option>
<option class="level-0" value="19">Chhattisgarh,Kirandul</option>
<option class="level-0" value="12">Chhattisgarh,Korba</option>
<option class="level-0" value="18">Chhattisgarh,Koriya</option>
<option class="level-0" value="27">Chhattisgarh,Kunkuri</option>
<option class="level-0" value="22">Chhattisgarh,Kusumkasa</option>
<option class="level-0" value="305">Chhattisgarh,Mahasamund</option>
<option class="level-0" value="17">Chhattisgarh,Neora</option>
<option class="level-0" value="28">Chhattisgarh,Pathalgaon</option>
<option class="level-0" value="10" selected="selected">Chhattisgarh,Raigarh</option>
<option class="level-0" value="3">Chhattisgarh,Raipur</option>
<option class="level-0" value="20">Chhattisgarh,Rajnandgaon</option>
<option class="level-0" value="8">Chhattisgarh,Surguja</option>
<option class="level-0" value="240">Raipur</option>
<option class="level-0" value="1">Uncategorized</option>
</select>
EOFL;
$vals = extract_options($a);
echo print_r($vals, true);
// Output:
Array
(
[1] => Uncategorized
[3] => Chhattisgarh,Raipur
[4] => Chhattisgarh,Jagdalpur
[5] => Ambikapur,Chhattisgarh
[6] => Bilaspur,Chhattisgarh
[7] => Chhattisgarh,Durg
[8] => Chhattisgarh,Surguja
[9] => Bastar,Chhattisgarh
[10] => Chhattisgarh,Raigarh
[11] => Chhattisgarh,Kanker
[12] => Chhattisgarh,Korba
[13] => Baikunthpur,Chhattisgarh
[14] => Chhattisgarh,Dhamtari
[15] => Chhattisgarh,Jashpur
[16] => Bhilai,Chhattisgarh
[17] => Chhattisgarh,Neora
[18] => Chhattisgarh,Koriya
[19] => Chhattisgarh,Kirandul
[20] => Chhattisgarh,Rajnandgaon
[21] => Bacheli,Chhattisgarh
[22] => Chhattisgarh,Kusumkasa
[23] => Chhattisgarh,Dantewada
[25] => Chhattisgarh
[26] => Anjora,Chhattisgarh
[27] => Chhattisgarh,Kunkuri
[28] => Chhattisgarh,Pathalgaon
[29] => Chhattisgarh,Janjgir-Champa
[240] => Raipur
[305] => Chhattisgarh,Mahasamund
)
*/
?>