Modification to change the input from array to bulk code. (Code provided and working)
how can I modify my function to make use of
<select name="extra2" id="extra2" class="select_smaller">
<option value="Algema">Algema</option>
<option value="Barkas">Barkas</option>
.
.
.
</select>
that I have on my forms and then creates the array input of the function below to get it work ?
$options = array('Algema', 'Barkas', 'Cadillac', …);
If this is not possible or a big thing, we can avoid the "get from dropdown list and create me an array input" and just use the dropdown list as is, to produce my desired output.
The result is correct, the code works great but what I want is to avoid the copy-paste around 200 different dropdown lists with about 10 options each. Instead I will use a program for mass text input to paste the code in the front and end of the lists.
function makeSelect($name, $options) {
foreach ($options as &$option) {
$selected = isset($_GET[$name]) && $_GET[$name] == $option;
$option = sprintf('<option value="%1$s"%2$s>%1$s</option>',
htmlspecialchars($option),
$selected ? ' selected="selected"' : null);
}
return sprintf('<select name="%1$s" id="%1$s" class="select">%2$s</select>',
htmlspecialchars($name),
join($options));
}
$options = array('Algema', 'Barkas', 'Cadillac', …);
// instead of array I prefer to use here something $options=the dropdown list as is.
ech开发者_如何转开发o makeSelect('car', $options);
Use regular expressions, for example (assuming $list_html
contains HTML in the form you have cited):
$count = preg_match_all('/<option value=\"([a-zA-Z0-9]*)\"\>/', $list_html, $matches);
if ($count) {
// something has been found
$found_values = $matches[1];
} else {
$found_values = array();
}
This has been tested. If you assign value in the following way:
$list_html = '<select name="extra2" id="extra2" class="select_smaller">'
.'<option value="Algema">Algema</option>'
.'<option value="Barkas">Barkas</option>'
.'</select>';
and do print_r($found_values)
, the result will be:
Array ( [0] => Algema [1] => Barkas )
And that means, you get proper values for each option in array. Assuming of course, that these values contain small letters, big letters or ciphers, but nothing more (otherwise the regular expression has to be adjusted to meet your needs).
EDIT:
For your convenience, the same thing in form of a function:
/**
* Get all values of 'option' tags in given HTML
* @param string $list_html
* @return array values of option tags or empty array if none
*/
function extractOptionValues($list_html) {
$regex = '/<option value=\"([a-zA-Z0-9]*)\"\>/';
$count = preg_match_all($regex, $list_html, $matches);
if ($count) {
$found_values = $matches[1];
} else {
$found_values = array();
}
return $found_values;
}
Now it should be ok to do it in the following way:
$options = extractOptionValues($list_html); // $list_html contains select HTML
EDIT 2:
The same mechanism included within your function could look like that:
/**
* Return HTML of select field with one option selected, built based
* on the list of options provided
* @param mixed $options array of options or HTML of select form field
* @return string HTML of the select field
*/
function makeSelect($name, $options) {
if (is_string($options)) {
// assuming the options string given is HTML of select field
$regex = '/<option value=\"([a-zA-Z0-9]*)\"\>/';
$count = preg_match_all($regex, $options, $matches);
if ($count) {
$options = $matches[1];
} else {
$options = array();
}
}
foreach ($options as &$option) {
$selected = isset($_GET[$name]) && $_GET[$name] == $option;
$option = sprintf('<option value="%1$s"%2$s>%1$s</option>',
htmlspecialchars($option),
$selected ? ' selected="selected"' : null);
}
return sprintf('<select name="%1$s" id="%1$s" class="select">%2$s</select>',
htmlspecialchars($name),
join($options));
}
精彩评论