DEV Community

Discussion on: Daily Challenge #305 - Remove Anchors from URLs

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with parse_url function in PHP:

function replaceAll($string) {
  $parsed = parse_url($string);

  $scheme = $parsed['scheme'] ?? false;

  if ($scheme !== false) {
    return $scheme . '://' . $parsed['host'];
  }

  return $parsed['path'];
}
Enter fullscreen mode Exit fullscreen mode