1

I'm in the process of creating a website. Every time the user uploads a link, I need to save the link and its name/value. This is hard to explain.

Here's what I'm trying to say. Lets say the user pastes a link in an input. https://www.google.com/ The link needs to be saved in an XML file. When I call the link:

<a href="<?php whatever the php is ?>"> </a>

I want to also call the name:

<a href="<?php whatever the php is ?>">Google</a>

I want to extract "google" from "https://www.google.com/"

I know I could use:

str_replace(' ', '', )

But not all links are the same. I know this code won't work because I already tried it. I know the right code is simple probably 3 lines. So I would really appreciate it if you guys could help me out.

Thank you.

kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51
user3242704
  • 37
  • 1
  • 8
  • possible duplicate of [Get domain name from full URL](http://stackoverflow.com/questions/16027102/get-domain-name-from-full-url) – Michal Brašna Feb 06 '14 at 22:13
  • This thread may help. http://stackoverflow.com/questions/12787863/remove-base-url-from-a-link-in-a-string – mdurchholz Feb 06 '14 at 22:20
  • No dats not what i want. i know how to tho what the persons want in the other article.. this is a little diffrent.. not every links are from the same websites – user3242704 Feb 06 '14 at 22:22
  • This is a very difficult task to achieve, given the various ways domains can be structured, E.G http://domain.com, http://subdomain.domain.com, http://domain.co.uk, http://subdomain.domain.co.uk etc. You will need to create a function that strips out every known tld, and remove any subdomain (including www) – Pierre Feb 06 '14 at 22:45

1 Answers1

2

parse the url. get the host part and explode the string with a dot (.) as delimiter.

$url="https://google.com/";
$parts = parse_url($url);
$parts=explode('.',$parts['host']);
echo $parts[0]; // parts[1] contains com, parts[0] contains google

To work with urls that contain 'www' you might do something like this. Mind that this is not working with subdomains.

echo getName("http://www.google.com"); //prints google

function getName($url){

    $parts = parse_url($url);
    $parts=explode('.',$parts['host']);
    $data=$parts[0]=="www"?$parts[1]:$parts[0];
    return $data;
}

To work with every url, including subdomains I think it is more easy to use a regex. I adapted the function from this question to fulfil your needs.

function getName($url){

  $pieces = parse_url($url);
  $domain = isset($pieces['host']) ? $pieces['host'] : '';
  if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs)) {
    return explode('.',$regs['domain'])[0];
  }
  return false;
}
Community
  • 1
  • 1
kasper Taeymans
  • 6,950
  • 5
  • 32
  • 51