From 8436f02ec02d916177a23250ae32b34ae5316942 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Tue, 16 Nov 2021 13:44:23 -0800 Subject: [PATCH] remove www from display url --- src/url.php | 4 ++-- tests/URLTest.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/url.php b/src/url.php index fd43abb..231e860 100644 --- a/src/url.php +++ b/src/url.php @@ -2,8 +2,8 @@ namespace p3k\url; function display_url($url) { - # remove scheme - $url = preg_replace('/^https?:\/\//', '', $url); + # remove scheme and www. + $url = preg_replace('/^https?:\/\/(www\.)?/', '', $url); # if the remaining string has no path components but has a trailing slash, remove the trailing slash $url = preg_replace('/^([^\/]+)\/$/', '$1', $url); return $url; diff --git a/tests/URLTest.php b/tests/URLTest.php index f650d5b..3fc0d4a 100644 --- a/tests/URLTest.php +++ b/tests/URLTest.php @@ -12,6 +12,12 @@ class URLTest extends PHPUnit_Framework_TestCase { $this->assertEquals('example.com/foo', $url); $url = p3k\url\display_url('http://example.com/foo/'); $this->assertEquals('example.com/foo/', $url); + $url = p3k\url\display_url('http://www.example.com/foo/'); + $this->assertEquals('example.com/foo/', $url); + $url = p3k\url\display_url('https://www.example.com/foo/'); + $this->assertEquals('example.com/foo/', $url); + $url = p3k\url\display_url('https://example.www.example.com/foo/'); + $this->assertEquals('example.www.example.com/foo/', $url); } public function testAddQueryParamsToURLNoExistingParams() {