简体   繁体   中英

In_array doesn't see element in DateTimeZone::ALL_WITH_BC, why?

Sorry for my english (Google translator helps)))

The general task is to check the presence of a time zone in the received cookie. If "Found", send it to the variable, if not "Not Found", the default variable.

I display a list of DateTimeZone::ALL_WITH_BC timezones, when the list changes, a cookie with value is created, by in_array, a check in the DateTimeZone::ALL_WITH_BC array. But values with a plus sign + do not pass the check, ie Etc/GMT+3, etc.

Sample code:

<?php
echo '<select id="List">';
$tzlist = DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC);
foreach($tzlist as $value)
{
echo '<option value='. $value .'>'. $value .'</option>';
}
echo '</select>';
$takeCookie = $_COOKIE['cookie'];
if (in_array($takeCookie, DateTimeZone::listIdentifiers(DateTimeZone::ALL_WITH_BC))) 
{
echo "Found";
} 
else {
echo "Not Found";
}
?>
<script>
  $("#List").change(function() {
  var tz = $(this).val();
  document.cookie = "cookie="+tz;
    });
</script>

When choosing the time zone and refreshing the page, all values are "Found", except for those with +, why?

For example, select from the list:

<option value="Etc/GMT+9">Etc/GMT+9</option>

Cookie type:

cookie=Etc/GMT+9

Example code above shows "Not Found".

UPD Ok, the problem is not In_array))

The problem is here:

<script>
    document.cookie = "cookie=Etc/GMT+9";
</script>

<?php
var_dump($_COOKIE['cookie']);
?>

Result:

string(9) "Etc/GMT 9"

Where is the plus (+) sign?

Thank you @Jeto, helped using encodeURIComponent. Like:

<script>
     document.cookie = "cookie="+encodeURIComponent('Etc/GMT+9');
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM