You are here

public function HelperService::parsePlaceUrl in Weather 2.0.x

Same name and namespace in other branches
  1. 8 src/Service/HelperService.php \Drupal\weather\Service\HelperService::parsePlaceUrl()

Parse full link to the place at yr.no site and return useful info.

Parameters

string $url: Full url to the place e.g: https://www.yr.no/place/Ukraine/Kiev/Kyiv/.

Return value

array Array with two parsed values: $country - Country of this place (e.g Ukraine or United_States). $link - Internal used link (e.g. Kiev/Kyiv, Maryland/Baltimore)

File

src/Service/HelperService.php, line 203

Class

HelperService
Some small helper functions.

Namespace

Drupal\weather\Service

Code

public function parsePlaceUrl(string $url) : array {

  // Remove "http://www.yr.no/place/" or "https://www.yr.no/place/"
  // from the URL.
  $url = str_replace('http://www.yr.no/place/', '', $url);
  $url = str_replace('https://www.yr.no/place/', '', $url);

  // Split by slashes and remove country (first item)
  // and "forecast.xml" (last item).
  $parts = explode('/', $url);

  // Remove country.
  $country = array_shift($parts);

  // Remove "forecast.xml".
  array_pop($parts);
  $link = implode('/', $parts);
  return [
    $country,
    $link,
  ];
}