Skip to content

Commit

Permalink
Merge pull request #7 from LordWilbur/htmlencode
Browse files Browse the repository at this point in the history
Add optional parameter to encode html entities
  • Loading branch information
JackieDo authored Apr 12, 2020
2 parents 2b787fd + d8a864e commit b9629de
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/Jackiedo/Timezonelist/Timezonelist.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ class Timezonelist
*
* @return string
*/
protected function formatTimezone($timezone, $continent)
protected function formatTimezone($timezone, $continent, $htmlencode=true)
{
$time = new DateTime(null, new DateTimeZone($timezone));
$offset = $time->format('P');
$offset = str_replace('-', ' − ', $offset);
$offset = str_replace('+', ' + ', $offset);
if ($htmlencode) {
$offset = str_replace('-', ' − ', $offset);
$offset = str_replace('+', ' + ', $offset);
}

$timezone = substr($timezone, strlen($continent) + 1);
$timezone = str_replace('St_', 'St. ', $timezone);
Expand All @@ -75,7 +77,7 @@ protected function formatTimezone($timezone, $continent)
* @param mixed $attr
* @return string
**/
public function create($name, $selected='', $attr='')
public function create($name, $selected='', $attr='', $htmlencode=true)
{

// Attributes for select element
Expand Down Expand Up @@ -113,7 +115,7 @@ public function create($name, $selected='', $attr='')
$selected_attr = ($selected == $timezone) ? ' selected="selected"' : '';

$listbox .= '<option value="' .$timezone. '"' .$selected_attr. '>';
$listbox .= $this->formatTimezone($timezone, $continent);
$listbox .= $this->formatTimezone($timezone, $continent, $htmlencode);
$listbox .= '</option>';
}

Expand All @@ -132,7 +134,7 @@ public function create($name, $selected='', $attr='')
*
* @return mixed
**/
public function toArray()
public function toArray($htmlencode=true)
{
$list = [];

Expand All @@ -145,7 +147,7 @@ public function toArray()
foreach ($this->continents as $continent => $mask) {
$timezones = DateTimeZone::listIdentifiers($mask);
foreach ($timezones as $timezone) {
$list[$continent][$timezone] = $this->formatTimezone($timezone, $continent);
$list[$continent][$timezone] = $this->formatTimezone($timezone, $continent, $htmlencode);
}
}

Expand Down

0 comments on commit b9629de

Please sign in to comment.