Expand on an existing function

I found an old function which partly does what I need to do however my expertise is quite limited and am not able to expand on it to make work for my entire needs. Here’s the code:

function cleanUrl($url)
{
  if (($d= parse_url($url)) !== false) // valid url
  {
    return sprintf('%s%s%s',
      ltrim($d['host'], 'www.'),
      rtrim($d['path']. '/'),
      !empty($d['query']) ? '?'.$d['query'] : '');
  }
  return $url;
}

how can expand on this to also remove the entire path and just leave stripped down domain name?

Currently it takes, http://www.example.com/foo/bar and returns example.com/foo/bar, I’d like just example. Perhaps I can use the parse_url function to achieve this but I don’t quite know how to go about it.

Try this.

function cleanUrl($url) {

    /*
    // valid url 
    if(($d = parse_url($url)) !== false) {
        return sprintf('%s%s%s',
        ltrim($d['host'], 'www.'),
        rtrim($d['path']. '/'),
        !empty($d['query']) ? '?'.$d['query'] : '');
    }
    */

    if(strpos($url, 'www.') !== false) {

        $url = substr($url, 0, strpos($url, '.', strpos($url, '.')+1));
        $array = array('http://' => '', 'https://' => '', '//' => '', 'www.' => '', ' ' => '', '    ' => '');
        $url = strtr($url, $array);

        return $url;

    } else {

        $url = substr($url, 0, strpos($url, '.', strpos($url, '.')));
        $array = array('http://' => '', 'https://' => '', '//' => '', ' ' => '', '    ' => '');
        $url = strtr($url, $array);

        return $url;

    }

}

I commented out your stuff because it looks to rely on finding certain parts of the URL which kind of doesn’t work because in a URL, there can be more than 1 path.

In the snippet I modified for you, you check to see if the URL contains www. using strpos. If the string contains www., then you find the second period and remove everything after it. Next, with a little combining, you can take the string that you have left and remove the www. along with http://, https://, and // using an array. You’ll end up with example. However, if the string doesn’t contain www., you go to the second statement and find the first period instead of the second. Then, remove everything after the first period along with http://, https://, and // using an array. Again, you’ll end up with example.

Hope this helps.

Also, I forgot to tell you to remove all spacing so you end up with

example

instead of

 example

Would something like this work even with its limitations?
http://php.net/manual/en/function.parse-url.php

function return_scheme_and_host($url_string) {
  $scheme = "";
  $host = "";
  $scheme .= parse_url($url_string, PHP_URL_SCHEME);
  $host .= parse_url($url_string, PHP_URL_HOST);
  return $scheme . "://" . $host;
}

echo return_scheme_and_host($url);

Unless I am missing the point and overlooking the obvious. If you only require example returning then try this:

function cleanUrl($url)
{
   return 'example';
}

:laughing: I too might be overlooking what could be simple. I thought OP wants to have a dynamic return. So the string could change at any time to whatever the OP wants. So if they want to have let’s say example returned to them and a few days later they want something like facebook returned to them, then the snippet I provided does a pretty well job of it. Could be wrong, but it’s a start.

If all you want is “example” from the above URL.

<?php

$url = "http://www.example.com/foo/bar";

function return_host_name($url) {

   $host = parse_url($url, PHP_URL_HOST);
   $host_fragments = explode(".", $host);
   if ( count($host_fragments) > 2 ) {
      return $host_fragments[1];
   }else{
      return $host_fragments[0];
   }
}

echo return_host_name($url);

Scott

1 Like

Thanks to all for the replies, @s_molinari your one works perfectly as expected. Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.