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);
}
Related
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);
This question already has answers here:
Get Last Part of URL PHP
(14 answers)
Closed 1 year ago.
If I have a URL look like this:
http://wwww.example/test1/test2/test3/
How can I retrieve string test3 from the url above?
$str = explode("/","http://wwww.example/test1/test2/test3/");
echo $str[count($str)-2];
DEMO: https://eval.in/83914
Regular expression solution.
$url = 'http://wwww.example/test1/test2/test3/';
preg_match('#.*/(.*)/$#', $url, $matches);
echo $matches[1];
Demo
Using capturing group, $:
preg_match('!/([^/]+)/[^/]*$!', 'http://wwww.example/test1/test2/test3/', $matches);
echo $matches[1];
DEMO: http://ideone.com/7EVfZa
So basename() does exactly that.
$url = 'http://www.example/test1/test2/test3/';
echo basename($url); // returns : test3;
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']);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regex, get string value between two characters
myemail#gmail.com
What is the pattern to get what's between '#' and '.' in this string?
In this case it would get 'gmail'.
I believe it is ...
preg_match("'#(.*?)\.'si", $source, $match);
$email = 'myemail#gmail.com';
$gmail = preg_match('/#([^\.]*)\./', $email, $m) ? $m[1] : false;
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];