Browse Source

remove www from display url

main 1.2.1
Aaron Parecki 6 months ago
parent
commit
8436f02ec0
2 changed files with 8 additions and 2 deletions
  1. +2
    -2
      src/url.php
  2. +6
    -0
      tests/URLTest.php

+ 2
- 2
src/url.php View File

@ -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;

+ 6
- 0
tests/URLTest.php View File

@ -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() {

Loading…
Cancel
Save