: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* @author Michael Pratt <yo@michael-pratt.com>
* @link http://www.michael-pratt.com/
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
* This class is used to preform common/popular
* tasks into a url string
/** @var string The url */
/** @var string Placeholder for the original url */
* @param string $url A valid url string
public function __construct($url)
$this->originalUrl = $url;
* Returns the full url when
* the object is casted into a string
public function __toString()
if (!in_array(strtolower($this->parts['scheme']), ['http', 'https'])) {
$this->parts['scheme'] = 'https';
$url[] = $this->parts['scheme'] . '://' . $this->parts['host'] . $this->parts['path'];
if (!empty($this->parts['query'])) {
$url[] = '?' . $this->parts['query'];
if (!empty($this->parts['fragment'])) {
$url[] = '#' . $this->parts['fragment'];
return implode('', $url);
* Overwrites the value of $this->url with
public function overwrite($url)
$this->parts = array_merge([
], (array) parse_url($url));
* Discards changes made to a url, and goes back to the original
public function discardChanges()
$this->__construct($this->originalUrl);
* Change the host of the url
public function setHost($host)
$this->parts['host'] = $host;
* Strips the query string from the url
public function removeQueryString()
$this->parts['query'] = $this->parts['fragment'] = false;
* Strips the / at the end of a url
public function removeLastSlash()
$this->parts['path'] = rtrim($this->parts['path'], '/');
* Strips starting www from the url
public function removeWWW()
$this->parts['host'] = preg_replace('~^www\.~i', '', $this->parts['host']);
* Adds www. subdomain to the urls
if (!preg_match('~^www\.~i', $this->parts['host'])) {
$this->parts['host'] = 'www.' . $this->parts['host'];
* Replaces https protocol to http
public function convertToHttp()
$this->parts['scheme'] = 'http';
* Replaces http protocol to https
public function convertToHttps()
$this->parts['scheme'] = 'https';