修改以将输入从数组更改为批量代码。 (提供的代码和工作)

时间:2011-05-13 18:22:28

标签: php regex

如何修改我的功能以使用

<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);

1 个答案:

答案 0 :(得分:1)

例如,使用正则表达式(假设$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));
}