修改以将输入从数组更改为批量代码.(提供代码并正常工作)


Modification to change the input from array to bulk code. (Code provided and working)

如何修改我的函数以使用

<select name="extra2"  id="extra2" class="select_smaller">
    <option value="Algema">Algema</option>
    <option value="Barkas">Barkas</option>
.
.
.
</select>

我在我的表单上,然后创建下面函数的数组输入以使其工作? $options = array('Algema', 'Barkas', 'Cadillac', …);

如果这是不可能的或一件大事,我们可以避免"从下拉列表中获取并创建我一个数组输入"和只需按原样使用下拉列表即可生成我想要的输出。

结果是正确的,代码效果很好,但我想要的是避免复制粘贴大约 200 个不同的下拉列表,每个列表大约有 10 个选项。相反,我将使用一个程序进行大量文本输入,将代码粘贴到列表的前面和末尾。

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.
    echo makeSelect('car', $options);

例如,使用正则表达式(假设$list_html包含您引用的表单中的 HTML):

$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();
}

这已经过测试。如果按以下方式分配值:

$list_html = '<select name="extra2"  id="extra2" class="select_smaller">'
    .'<option value="Algema">Algema</option>'
    .'<option value="Barkas">Barkas</option>'
    .'</select>';

并执行print_r($found_values),结果将是:

Array ( [0] => Algema [1] => Barkas )

这意味着,您可以为数组中的每个选项获得正确的值。当然,假设这些值包含小写字母、大字母或密码,但仅此而已(否则必须调整正则表达式以满足您的需求)。

编辑:

为了您的方便,函数形式的相同内容:

/**
 * 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;
}

现在应该可以通过以下方式执行此操作:

$options = extractOptionValues($list_html); // $list_html contains select HTML

编辑2:

函数中包含的相同机制可能如下所示:

/**
 * 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));
}