简化多重回波

发布于 2024-09-07 11:56:53 字数 866 浏览 2 评论 0原文

我在选择菜单中有完整的时区列表,如下所示:

<option value="Pacific/Kosrae"> Pacific/Kosrae( +11:00 GMT ) </option>
  <option value="Pacific/Kwajalein"> Pacific/Kwajalein( +12:00 GMT ) </option>
  <option value="Pacific/Majuro"> Pacific/Majuro( +12:00 GMT ) </option>
  <option value="Pacific/Marquesas"> Pacific/Marquesas( -09:30 GMT ) </option>
  <option value="Pacific/Midway"> Pacific/Midway( -11:00 GMT ) </option>

该列表永远持续下去。

我想将每个选项更改为以下格式:

if($_SESSION['timezone'] == 'Africa/Abidjan') {
echo '<option selected="selected" value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>'; 
} else {
echo '<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
}

How can I use php to避免手动复制粘贴和编辑每个选项?

I have a full list of timezones in a select menu like so:

<option value="Pacific/Kosrae"> Pacific/Kosrae( +11:00 GMT ) </option>
  <option value="Pacific/Kwajalein"> Pacific/Kwajalein( +12:00 GMT ) </option>
  <option value="Pacific/Majuro"> Pacific/Majuro( +12:00 GMT ) </option>
  <option value="Pacific/Marquesas"> Pacific/Marquesas( -09:30 GMT ) </option>
  <option value="Pacific/Midway"> Pacific/Midway( -11:00 GMT ) </option>

the list goes on forever.

I want to change each of the options into this format:

if($_SESSION['timezone'] == 'Africa/Abidjan') {
echo '<option selected="selected" value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>'; 
} else {
echo '<option value="Africa/Abidjan"> Africa/Abidjan( +00:00 GMT ) </option>';
}

How can I use php to avoid having to copy paste and edit each of the options manually??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

国际总奸 2024-09-14 11:56:53

将数据存储在某种数据结构中,并使用循环。例如,使用从时区名称到偏移量的映射:

$timezones = array(
    'Pacific/Kosrae' => '+11:00',
    'Pacific/Kwajalein' => '+12:00',
    ...
);

foreach($timezones as $name => $offset) {
    echo "<option value=\"$name\"" . ($name == $_SESSION['timezone'] ? " selected" : "") . ">$name( $offset GMT ) </option>\n";
}

Store the data in some data structure, and use a loop. For example, using a map from timezone name to offset:

$timezones = array(
    'Pacific/Kosrae' => '+11:00',
    'Pacific/Kwajalein' => '+12:00',
    ...
);

foreach($timezones as $name => $offset) {
    echo "<option value=\"$name\"" . ($name == $_SESSION['timezone'] ? " selected" : "") . ">$name( $offset GMT ) </option>\n";
}
牵强ㄟ 2024-09-14 11:56:53

好吧,假设您有一个包含上述表单的变量,我们将其称为 $form ,另一个变量包含 'Africa/Abidjan' - $timezone.

$pattern = '/="'.str_replace('/', '\/', $timezone).'"/'; # /="Africa\/Abidjan"/
$replacement = '="'.$timezone.'" selected="selected"'; # ="Africa/Abidjan" selected="selected"
$output_form = preg_replace($pattern, $replacement, $form);

还没有实际测试过,但它应该可以工作。

Ok, imagine you have a variable containing the form above, let's call it $form and another variable containing i.e. 'Africa/Abidjan' - $timezone.

$pattern = '/="'.str_replace('/', '\/', $timezone).'"/'; # /="Africa\/Abidjan"/
$replacement = '="'.$timezone.'" selected="selected"'; # ="Africa/Abidjan" selected="selected"
$output_form = preg_replace($pattern, $replacement, $form);

Haven't actually tested it, but it should work.

放赐 2024-09-14 11:56:53
$cur_timezone = 'Africa/Abidjan';
$timezones_arr = array ('Pacific/Kosrae','Pacific/Kwajalein',...);
$times_arr = array ('+11:00 GMT', '+12:00 GMT',...);
for ($i = 0; $i < count ($timezones_arr); $i ++) {
  if ($timezones_arr[$i] == $cur_timezone) {
    echo '<option selected="selected" value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
  else {
    echo '<option value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
}

您必须仅更改变量 $cur_timezone。对于 $timezones_arr 的每个元素都必须存在 $times_arr 的元素。

$cur_timezone = 'Africa/Abidjan';
$timezones_arr = array ('Pacific/Kosrae','Pacific/Kwajalein',...);
$times_arr = array ('+11:00 GMT', '+12:00 GMT',...);
for ($i = 0; $i < count ($timezones_arr); $i ++) {
  if ($timezones_arr[$i] == $cur_timezone) {
    echo '<option selected="selected" value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
  else {
    echo '<option value='$timezones_arr[$i]'>$timezones_arr[$i]($times_arr[$i]) </option>';
  }
}

You must change only variable $cur_timezone. For every element of $timezones_arr must exist elemnt of $times_arr.

画尸师 2024-09-14 11:56:53

好吧,你不必使用 DOM,但自从已经有很多其他答案显示了不同的方法,以下是如何使用 DOM 来做到这一点:

function timezoneHelper($selected = NULL)
{   
    $dom = new DOMDocument;
    $dom->formatOutput = TRUE;
    $dom->loadXML('<select/>');
    $dom->documentElement->setAttribute('name', 'timezone-selector');
    $timezones = DateTimeZone::listIdentifiers();
    if(!is_numeric($selected)) {
        $selected = array_search($selected, $timezones);
    }
    foreach($timezones as $id => $timezone) {
        $option = $dom->createElement('option', $timezone);
        $option->setAttribute('value', $id);
        if($id == $selected) {
            $option->setAttribute('selected', 'selected');
        }
        $dom->documentElement->appendChild($option);
        unset($option);
    }
    return $dom->saveXML($dom->documentElement);
}

上面将创建一个时区标识符列表(尽管没有 GMT 差异。请参阅 DateTimeZone::listAbbreviations(如果需要)作为

echo timezoneHelper('551');
// or
echo timezoneHelper('Zulu');

两者都会

 <option value="551" selected="selected">Zulu</option>

在返回的列表中使用 selected 属性进行标记。

Ok, you don't have to it with DOM, but since there is already plenty of other answers showing different approaches, here is how to do it with DOM:

function timezoneHelper($selected = NULL)
{   
    $dom = new DOMDocument;
    $dom->formatOutput = TRUE;
    $dom->loadXML('<select/>');
    $dom->documentElement->setAttribute('name', 'timezone-selector');
    $timezones = DateTimeZone::listIdentifiers();
    if(!is_numeric($selected)) {
        $selected = array_search($selected, $timezones);
    }
    foreach($timezones as $id => $timezone) {
        $option = $dom->createElement('option', $timezone);
        $option->setAttribute('value', $id);
        if($id == $selected) {
            $option->setAttribute('selected', 'selected');
        }
        $dom->documentElement->appendChild($option);
        unset($option);
    }
    return $dom->saveXML($dom->documentElement);
}

The above will create a list of timezone identifiers (w\out the GMT diff though. See DateTimeZone::listAbbreviations if you need them) as <option> elements in a <select> element. Unlike your code in the question, the value used for the value attribute is the numeric offset in the array returned by DateTimeZone::listIdentifiers(); instead of the timezone identifier itself. You can invoke the helper with the timezone identifier or the numeric ID though, e.g.

echo timezoneHelper('551');
// or
echo timezoneHelper('Zulu');

would both markup

 <option value="551" selected="selected">Zulu</option>

with the selected attribute in the list returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文