SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: spliting URLs after a domain
-
Mar 15, 2001, 19:11 #1
- Join Date
- Oct 2000
- Posts
- 430
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi,
I need to extract the first part of a URL from a varaible and would like your opinion on the best option.
the end result variable needs to be in the following format:
$EndVariable = sitename.domain;
and the start URL could be anything such as:
http://www.sitename.domain/home.php?v=10&x=2
Getting rid of everything before the "sitename" (ie http:// and www.) is easily done by str_replace ("http://", "", $URL); etc
but the problem comes with getting rid of everything after the domain (which could be .com, .net, .co.uk, .uk.com etc etc).
I was considering (after removing http://, www. etc from the $URL) using
$URL = explode("/", $URL);
$URL = explode("?", $URL[0]);
$EndVariable = $URL[0];
to get rid of everything after the domain, but I'm concerned that there may be URL's that contain other charactors directly after the domain.
Is the above a decent option? And if so what other charactors occasionally occur straight after the domain?
I was thinking about using a regex, however I can't forsee any advantage over the above, and you still have the same "where to split problem".
Cheers for the help
-
Mar 15, 2001, 20:31 #2
- Join Date
- Feb 2001
- Location
- Monmouth Junction, NJ
- Posts
- 88
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
its http:// there are 2 forward slashes
-
Mar 15, 2001, 20:32 #3
- Join Date
- Feb 2001
- Location
- Monmouth Junction, NJ
- Posts
- 88
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
id suggest splitting at the
$URL = explode("w", $URL);
or something
-
Mar 15, 2001, 20:35 #4
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
how about
PHP Code:$URL = explode(".", $URL);
$newvar = $URL[1].".".$URL[2];
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Mar 16, 2001, 07:01 #5
- Join Date
- Oct 2000
- Posts
- 430
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cheers Freddie,
But the problem with exploding the "." is that you run into problems with names such as http://uk.site.com, http://www.site.co.uk etc etc, as various numbers of "." may occur.
I think my way is 99.5% foolproof and will only break if you get odd charactors straight after the domain or obscure "www." (eg www2 etc):
$URL = str_replace("http://", "", $URL);
$URL = str_replace("https://", "", $URL);
$URL = str_replace("ftp://", "", $URL);
$URL = str_replace("www.", "", $URL);
$URL = str_replace("www1.", "", $URL);
$URL = explode("/", $URL);
$URL = explode("?", $URL[0]);
$EndVariable = $URL[0];
To make it more foolproof can anyone suggest to me any charactors which occasionally occur straight after a domain (eg /, ?) or alternatives used besides www. of http:// or than those used.
Cheers
-
Mar 16, 2001, 12:16 #6
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here you go
PHP Code:function get_servername($url) {
$str = eregi("(http|https|mailto|ftp)://(.*)/",$url, $args);
$tmp = explode(".", $args[2]);
if (eregi("www", $tmp[0])) array_shift($tmp);
return implode(".", $tmp);
}
$url = "ftp://www2.dasfhsdjkf.com.uk/ehherftwejh";
print get_servername($url);
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Mar 16, 2001, 12:29 #7
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:function get_servername($url) {
$str = eregi("(http|https|mailto|ftp)://([^/]*)",$url, $args);
$tmp = explode(".", $args[2]);
if (eregi("www", $tmp[0])) array_shift($tmp);
return implode(".", $tmp);
}
$url = "ftp://www2.dasfhsdjkf.com.uk/ehherftwejh";
print get_servername($url);
Please don't PM me with questions.
Use the forums, that is what they are here for.
-
Mar 16, 2001, 15:59 #8
- Join Date
- Oct 2000
- Posts
- 430
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cheers Freddie,
The use of regex, is still a little over my head as I haven't used it much - Although I can kind of follow you.
-
Mar 16, 2001, 16:31 #9
- Join Date
- Aug 2000
- Location
- San Diego, CA
- Posts
- 5,460
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It is pretty simple. It means match anything after a http:// or ftp:// or mailto:// or https:// and beofre a slash.
Then take that match and store it in $args so since it was the second match we were looking for then it is in $args[2]
So now explode that by a .
Now look for www in the first element of the exploded array if is there then shift that element off the array and implode the array back together using .Please don't PM me with questions.
Use the forums, that is what they are here for.
Bookmarks