Getting domain extension from URL? [duplicate] - php

This question already has answers here:
Extract domain from url (including the hard ones) [duplicate]
(3 answers)
Closed 9 years ago.
I want to get the domain extension from the url. eg. .com, .net etc.
I have used this:
$extension = pathinfo($_SERVER['SERVER_NAME'], PATHINFO_EXTENSION);
I was just wondering if there was a better way using parse_url?

Use parse_url(). Something like:
$host = parse_url('http://www.google.co.uk/test.html');
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $host['host'], $m);
$ext = isset($m[2]) ? $m[2]: '';
Edit: Fixed with regex from this answer. Extension like .co.uk are supported too.

You can easily split the requested URI like this:
function tld( $uri ) {
$parts = explode('.', $uri);
return (sizeof($parts) ? ('.' . end($parts)) : false;
}
So you can do this:
if(!tld('googlecom')) // does not contain an ending TLD
if(tld('google.com')) // this is a valid domain name; output: ".com"
Or you can just use:
echo $_SERVER['SERVER_NAME'];
So in my oppinion the most best example of usage is:
echo tld($_SERVER['SERVER_NAME']);

Related

Get string from URL without slash [duplicate]

This question already has answers here:
Extracting the last segment on an URI
(4 answers)
Closed 6 years ago.
I have a URL like http://localhost/m/2016/05/05/sebut-gubernur.
If link is clicked, I want get sebut-gubernur.
The $_SERVER variable helps your. and also explode function.
$part = explode("/", urldecode($_SERVER['REQUEST_URI']));
echo $part[(count($part) - 1)]; //sebut-gubernur
Note: You also need .htaccess code for this kind of url.
OR
If you have the link as a string and want to get sebut-gubernur then try:
$link = 'http://localhost/m/2016/05/05/sebut-gubernur';
$part = explode("/", urldecode($link));
echo $part[(count($part) - 1)]; //sebut-gubernur
echo substr($url, strrpos($url, '/') + 1);

How to slice an input value? [duplicate]

This question already has answers here:
How do I get a file name from a full path with PHP?
(14 answers)
Closed 8 years ago.
My input value is just like this: Oppa/upload/default.jpeg
I want to slice the value of an input according by / cause i want to get the image file name. Does anyone know some tricks to do this?
example: i want to get default.png
<input type="text" value="Oppa/upload/default.png" id="fileLink" name="fileLink" />
Use basename():
$path = "Oppa/upload/default.jpeg";
echo basename($path); //will output "default.jpeg"
echo basename($path, '.jpeg'); //will output "default"
The first parameter is the path of which the trailing component will be removed. If the first parameter ends in the optional second parameter, the second parameter will also be cut off.
On Windows, both slash (/) and backslash (\) are used as directory
separator character. In other environments, it is the forward slash
(/).
- PHP manual
You should use basename() PHP function.
This will work for you
$path = "Oppa/upload/default.jpeg";
echo basename($path);
Use pathinfo() php function
$path = "http://domain.tld/Oppa/upload/default.png";
$info = pathinfo ( $path, PATHINFO_BASENAME ); // returns default.png
Yet another solution, using preg_match()
<?php
$path = "http://domain.tld/Oppa/upload/default.png"; // or "C:\\domain.tld\Oppa\upload\default.jpg";
$pattern = '/[\/|\\\\]((?:.(?!\/|\\\\))+)$/';
if(preg_match($pattern, $path, $matches)){
echo $matches[1]; // default.png or default.jpg
}
?>
Note: People claim to have problems using basename() with asian characters
I am supposing that if your image path will not be change from Oppa/upload/ than this Should work using explode :
$str = "Oppa/upload/default.jpeg";
$s= explode("Oppa/upload/",$str);
echo $s[1];
Another Best thing you can do with defining the relative path as a constant, so :
const path = "Oppa/upload/";
$str = "Oppa/upload/default.jpeg";
$s= explode(path,$str);
echo $s[1];
will also work.

regex URL cut question mark [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parsing Domain From URL In PHP
How do I parse a URL in PHP?
I have this code:
$url = preg_replace("/^(http:\/\/)*(www.)*/is", "", $url);
$url = preg_replace("/\/.*$/is" , "" ,$url);
It work good get domain name where after .com is / . I got problem if there is url like this:
http://www.something.com?id=21213
What do I need to add to regex to cut ?id=21213 so there remains only something.com
See the magical build in function of parse_url().
Using parse_url(), you can retrieve the domain name solely by:
$domain = parse_url($url, PHP_URL_HOST);
if(0 === strpos('www.', $domain)){
$domain = substr($domain, 4);
}

Remove Url from String by finding by .com [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strip All Urls From A Mixed String ( php )
i need a way to remove url strings from var but not with finding the . then remove it. no, i need it by finding http and finding .com or .net or .info then remove the whole link
here is my previoys code
function cleaner($url) {
$U = explode(' ',$url);
$W =array();
foreach ($U as $k => $u) {
if (stristr($u,'http') || (count(explode('.',$u)) > 1)) {
unset($U[$k]);
return cleaner( implode(' ',$U));
}
}
return implode(' ',$U);
}
$url = "$first";
but that one remove the . not .com
You can use preg_match method to catch the stuff between http and .com
preg_match('%^((https?://)|(www\.))([a-z0-9-].?)+(:[0-9]+)?(/.*)?$%i', $url);
I think the simplest way to accomplish this but not error free is:
$url="http://codepad.org/";
$arry=explode(".",$url);
$arry1= explode("//",$arry[0]);
echo $arry1[1];

Get extension from filename like variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to extract a file extension in PHP?
I have a variable $filename="filename.ext" or $filename="filena.m.e.ext" or so on.. How can i extract the extension (here ext) from the variable / string? The variable may change or may have more than one dots.. In that case, i want to get the part after the last dot..
see the answer :
$ext = pathinfo($filename, PATHINFO_EXTENSION);
You can use the path info interrogation.
$info = pathinfo($file);
where
$info['extension']
contains the extension
you could define a function like this:
function get_file_extension($filename)
{
/*
* "." for extension should be available and not be the first character
* so position should not be false or 0.
*/
$lastDotPos = strrpos($fileName, '.');
if ( !$lastDotPos ) return false;
return substr($fileName, $lastDotPos+1);
}
or you could use the Spl_FileInfo object built into PHP
You want to use a regular expression:
preg_match('/\.([^\.]+)$/', $filename);
You can test it out here to see if it gets you the result you want given your input.
There are many ways to do this, ie with explode() or with a preg_match and others.
But the way I do this is with pathinfo:
$path_info = pathinfo($filename);
echo $path_info['extension'], "\n";
You could explode the string using ., then take the last array item:
$filename = "file.m.e.ext";
$filenameitems = explode(".", $filename);
echo $filenameitems[count($filenameitems) - 1]; // .ext
// or echo $filenameitem[-1];

Categories