URL: http://www2.zippyshare.com/v/hlA8HjA9/file.html
I just create a database and I know how to get the filename after the /v/ but now I need to add another string to my database. It's a server.
They've got subdomain as a server and each file is on different server for example www3.zippyshare or www73.zippyshare.com
Now I've got something like this:
$myLink = $_POST['url'];
$var_url = parse_url($myLink);
$var_parts = explode('.', $var_url['host']);
$var = var_parts[0];
But the link save the "v" to my database..
I don't know how to get the subdomain and then skip the www so I will add only the number to my database.
I am not sure if I write it clear enough but I will be really happy if someone could help me.
I'd use this regular expression:
/^(?:https?:\/\/)?([\da-z-]+)\.[a-z]+\.[a-z\.]{2,6}\/v\/[a-z0-9].+\/([a-z0-9]+\.[a-z]+)$/
Do it in PHP like this:
$url = "http://www2.zippyshare.com/v/hlA8HjA9/file.html";
$pattern = "/^(?:https?:\/\/)www([\d0-9-]+)\.[a-z]+\.[a-z\.]{2,6}\/v\/([a-z0-9\/\.].+)$/";
preg_match($pattern, $url, $matches);
list(, $server, $file) = $matches;
The server will then be in $server, the filename will be in $file.
Proof: https://3v4l.org/hHWRm
Related
I want to get foldername without any file name from a url in php?
My url is http://w3schools.com/php/demo/learningphp.php?lid=1348
I only want to retrieve the http://w3schools.com/php/demo from the url?
How to do this? Please help.
Try this,
$URL = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
$URL_SEGMENTS = explode("/", $URL);
foreach($URL_SEGMENTS as $Segment){
echo $Segment;
}
explode() will separate the string with / and provide an array. So you will have all url segments in foreach loop and you use it or store it in string or array.
After your comment let me show the script which will return your desire url.
$Desired_URL = $URL_SEGMENTS[2].'/'.$URL_SEGMENTS[3].'/'.$URL_SEGMENTS[4];
echo $Desired_URL;
If the url is stored in $url:
$url = 'http://w3schools.com/php/demo/learningphp.php?lid=1348';
You can do a preg_replace like so:
print(preg_replace('/\/[^\/]*$/', '', $url));
http://w3schools.com/php/demo
That regex means to replace everything from a / and all characters that are not / ... [^/]* ... to the end of the string ... $ ... with an empty string. Just delete them.
I have difficulties in remove some slug from the url.
I have some urls like these below
http://domain1.com/so-section/upload/image1.jpg
http://domain2example.com/so-section/upload/image2.jpg
http://domain3place.com/so-section/upload/image3.jpg
http://domain4code.com/so-section/upload/image4.jpg
http://domain5action.com/so-section/upload/image5.jpg
http://domain6rack.com/so-section/upload/image5.jpg
Obviously, you will see domain are unsame, So I only would like to get "/so-section/upload/imagename.jpg" from the url. Would that be possible to remove whatever domain name come! Please help!
You can use parse_url and then get the path:
$url = parse_url("http://domain1.com/so-section/upload/image1.jpg");
echo $url["path"]; ///so-section/upload/image1.jpg
$pieces = explode('/', $url);
echo '/'. $pieces[4].'/'.$pieces[5].'/'.$pieces[6];
A simple solution to get everything after the domain name.
function GetJunk($url)
{
$start = explode("/",$url); //get everything between "/"
array_shift($start);array_shift($start);array_shift($start); //shift sections
$junk = implode("/",$start); //get everything after the new shifted sections
return $junk; // so-section/upload/image2.jpg
};
Then to use you simply:
echo GetJunk("http://domain2example.com/so-section/upload/image2.jpg");
Here is a working example: http://phpfiddle.org/main/code/4sr-zrm
Hope this helps!
I have following urls.
reservation.abchotel.com
booking.abchotels.org
abc1.abc.dev
I want to get sub domain from above urls.
ex:-
.abchotel.com
.abchotels.org
.abc.dev
How I do it? I'm using zend feamwork. Please help me. What is the best solution?
It seems you don't want the subdomain but the domain. Because what you listed are not the subdomains.
The following pieces of knowledge will enable you to successfully deal with domain names.
Zend_Validate_Hostname - also good to look at the code
PHP Server variables
String manipulation in general. Hostnames are easy to in- and explode since they're always delimited by dots.
PHP parse_url
See also[this question on SO on how to get subdomain(s) from an url.
If they are always in that form you could do something like the following (assuming $url is set).
$split_url = explode(".", $url);
$subdomain = ".".$split_url[1].".".$split_url[2];
Or did you want to know how to get the URL in the first place too, or to allow for more than 3-level domains?
A very simple way to get domain and subdomain :
$parts = explode('.', $_SERVER['HTTP_HOST']);
$domain = '.' . implode( '.', array_reverse(
array(
array_pop($parts),
array_pop($parts)
)
);
$subdomain = implode('.',$parts);
$url = $_SERVER["SERVER_NAME"];
$replace_domains = array(
".abchotel.com" => "",
".abchotels.org" => "",
".abc.dev" => "");
$url = str_replace(array_keys($replace_domains), array_values($replace_domains), $url);
echo $url;
I am trying to make a user submit link box. I've been trying all day and can't seem to get it working.
The goal is to make all of these into example.com... (ie. remove all stuff before the top level domain)
Input is $url =
Their are 4 types of url:
www.example.com...
example.com...
http://www.example.com...
http://example.com...
Everything I make works on 1 or 2 types, but not all 4.
How one can do this?
You can use parse_url for that. For example:
function parse($url) {
$parts = parse_url($url);
if ($parts === false) {
return false;
}
return isset($parts['scheme'])
? $parts['host']
: substr($parts['path'], 0, strcspn($parts['path'], '/'));
}
This will leave the "www." part if it already exists, but it's trivial to cut that out with e.g. str_replace. If the url you give it is seriously malformed, it will return false.
Update (an improved solution):
I realized that the above would not work correctly if you try to trick it hard enough. So instead of whipping myself trying to compensate if it does not have a scheme, I realized that this would be better:
function parse($url) {
$parts = parse_url($url);
if ($parts === false) {
return false;
}
if (!isset($parts['scheme'])) {
$parts = parse_url('http://'.$url);
}
if ($parts === false) {
return false;
}
return $parts['host'];
}
Your input can be
www.example.com
example.com
http://www.example.com
http://example.com
$url_arr = parse_url($url);
echo $url_arr['host'];
output is example.com
there's a few steps you can take to get a clean url.
Firstly you need to make sure there is a protocol to make parse_url work correctly so you can do:
//Make sure it has a protocol
if(substr($url,0,7) != 'http://' || substr($url,0,8) != 'https://')
{
$url = 'http://' . $url;
}
Now we run it through parse_url()
$segments = parse_url($url);
But this is where it get's complicated because the way domain names are constructed is that you can have 1,2,3,4,5,6 .. .domain levels, meaning that you cannot detect the domain name from all urls, you have to have a pre compiled list of tld's to check the last portion of the domain, so you then can extract that leaving the website's domain.
There is a list available here : http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1
But you would be better of parsing this list into mysql and then select the row where the tld matches the left side of the domain string.
Then you order by length and limit to 1, if this is found then you can do something like:
$db_found_tld = 'co.uk';
$domain = 'a.b.c.domain.co.uk';
$domain_name = substr($domain,0 - strlen($db_found_tld));
This would leave a.b.c.domain, so you have removed the tld, now the domain name would be extracted like so:
$parts = explode($domain_name);
$base_domain = $parts[count($parts) - 1];
now you have domain.
this seems very lengthy but I hope now you know that its not easy to get just the domain name without tld or sub domains.
Given this variable:
$variable = foo.com/bar/foo
What function would trim $variable to foo.com ?
Edit: I would like the function to be able to trim anything on a URL that could possibly come after the domain name.
Thanks in advance,
John
Working for OP:
$host = parse_url($url, PHP_URL_HOST);
The version of PHP I have to work with doesn't accept two parameters (Zend Engine 1.3.0). Whatever. Here's the working code for me - you do have to have the full URL including the scheme (http://). If you can safely assume that the scheme is http:// (and not https:// or something else), you could just prepend that to get what you need.
Working for me:
$url = 'http://foo.com/bar/foo';
$parts = parse_url($url);
$host = $parts['host'];
echo "The host is $host\n";
I'm using http://www.google.com/asdf in my example
If you're fine with getting the subdomain as well, you could split by "//" and take the 1th element to effectively remove the protocol and get www.google.com/asdf
You can then split by "/" and get the 0th element.
That seems ugly. Just brainstorming here =)
Try this:
function getDomain($url)
{
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED) === FALSE)
{
return false;
}
/*** get the url parts ***/
$parts = parse_url($url);
/*** return the host domain ***/
return $parts['scheme'].'://'.$parts['host'];
}
$variable = 'foo.com/bar/foo';
echo getDomain($variable);
You can use php's parse_url function and then access the value of the key "host" to get the hostname