I needed to strip the
http://www.
from a domain name and also anything following it such as
/example
so that i would just be left with yourdomain.com
I added the following code to a file:
$domain = HTTP_SERVER;
$domain_name = preg_replace('/^https?:\/\/(?:www\.)?/i', '', $domain);
But if i echo $domain_name I still get a url such as yourdomain.com/testsite
Can anyone see what i have done wrong here as it has not removed the /testsite and i thought i had got this right.
use this
$url = 'http://www.example.co.uk/directory/level1/last/page.html';
$parse= parse_url($url);
preg_match ("/\.([^\/]+)/", $parse['host'], $mydomain);
echo $mydomain[1];
This may be a hack that someone will disagree with, but i resolved the problem by using the following code.
$url = HTTP_SERVER;
$parse = parse_url($url);
$domain = $parse['host'];
$domain_name = preg_replace('/(?:www\.)?/i', '', $domain);
echo $domain_name;
If you can see a reason why this should not be used, please feel free to let me know. Always something new to learn :)
Related
I am having several urls, such as:
http://stackoverflow.com
https://google.com
facebook.com
cnn.com/
https://help.uber.com/
I would only like to get back for every url the middle part, such as:
facebook, cnn, uber, stackoverflow, google.
I tried the following, where $line is the url:
$parts = parse_url($line);
$path_parts = explode('/', $parts['path']);
echo $path_parts[count($path_parts)-1];
However, as a return I do not get anything echoed out. The urls are also correctly read in!
Any suggestions what I am doing wrong?
I appreciate your replies!
Try using host like this. if your giving url like cnn.com parse_url parse it and will store it in path index.
<?php
$line='https://help.uber.com/';
$parts = parse_url($line);
$path_parts = explode('.', isset($parts['host'])?$parts['host']:$parts['path']);
echo $path_parts[count($path_parts)-2];
?>
You have to get the details first and use preg_match to get the format . Check the code below and you will get your desired output .
<?php
$line = "stackoverflow.com" ;
$parts = parse_url($line);
$domain_name = isset($parts['host']) ? $parts['host'] : $parts['path'];
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain_name, $dom)){
$d_name = explode(".", $dom['domain']) ;
echo $d_name[0];
}
?>
I am having a web service which returns some images url, the service is working fine but the issue is that the url is not coming correctly.
My code is as given below
$obj->book_title = "test.jpg";
$url = "http://www.example.com/uploads/books/thumb/".$obj->book_title;
the above code outputs as
http:\/\/www.example.com\/uploads\/books\/thumb\/test.jpg
Can anyone please tell me some solution for this
the code listed should not do that, there is something else in your code that makes it output like that... you could try this
$obj->book_title = "test.jpg";
$url = "http://www.example.com/uploads/books/thumb/".$obj->book_title;
$url = str_replace("\\", "", $url);
echo $url;
I have many thousands of urls from which i only want to get name of domain for example
http://google.com
<?php
$url = 'http://google.com';
$host = parse_url($url);
echo '<pre>';
print_r($host['host']);
echo '</pre>';
**//Output google.com**
?>
but i only want to get google from http://google.com not google.com
please help thanks
Not particularaly elegant but something like this gets simply the domain name...
$url = 'http://dev.subdomain.google.com';
$host = parse_url($url,PHP_URL_HOST);
$pieces=explode( '.', $host );
$popped=array_pop( $pieces ); //remove tld extension from stack
if( strlen( $popped ) <= 3 ) array_pop( $pieces ); //tld was likely a multi-part ext like .co.uk so pop next element off stack too!
$domain=array_pop( $pieces );
echo $domain; // returns 'google'
$url = 'http://google.com';
$host = parse_url($url);
$host = strstr($host, '.com', true);
See php.net/strstr for more detailed information, of course there's other and properly better ways to do it.
Try below code
<?php
$full_url = parse_url('http://facebook.com');
$url = $full_url['host'];
$url_array = explode('.',$url);
echo $url_array[0];
?>
maybe you can fix it with a regex
$host = (preg_replace("#(http://)|(https://)|\.(com)|(co\.uk)|(fr)|(de)|(org)|(net)#", "", $host));
preg_replace : preg_replace manual (php.net)
test your regex : Debuggex
I use PHP.
I have an URL that looks like this
http://www.mydomain.com/mydir/mydir2/?something=hello
I want this:
http://www.mydomain.com
I did it like this but it feels like the wrong way to do it
To long and ugly.
$url = 'http://www.mydomain.com/mydir/mydir2/?something=hello';
$root_url_a = explode('/', $url);
$root_url = $root_url_a[0] . '//' . $root_url_a[2];
$root_url_clean = $root_url_a[2];
Suggestions
Regex?
Xpath?
Some for me unknown PHP function?
The shortest most correct way of doing it will get my vote.
Ok here is an example:
$url = parse_url('http://www.mydomain.com/mydir/mydir2/?something=hello');
echo $url->scheme.'://'.$url->host;
Is along the right lines.
Though technically this is not even right since depending on whether you send in a scheme or not for a url parse_url can actually change the way it assigns variables, so I wrote:
function return_url($url){
$parsed_url = parse_url($url);
if(!$parsed_url){
return false;
}
if(isset($parsed_url['scheme'])){
if(!isset($parsed_url['host'])){
return false;
}else{
return $parsed_url['scheme'].'://'.$parsed_url['host'];
}
}
if(isset($parsed_url['path'])){
return 'http://'.$parsed_url['path'];
}
return false;
}
I would do it like this:
$url = "http://www.mydomain.com/mydir/mydir2/?something=hello";
echo parse_url($url, PHP_URL_HOST);
// Would echo:
http://www.mydomain.com
I would say RTM ;)
http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_HOST);
?>
URL = http://company.website.com/pages/users/add/
How do i find the subdomain from this via PHP
Such that $subdomain = 'company'
And $url = '/pages/users/add/'
You'll want to take a look at PHP's parse_url. This will give you the basic components of the URL which will make it easier to parse out the rest of your requirements (the subdomain)
$url = 'http://company.website.com/pages/users/add/';
$url_parsed = parse_url($url);
$path = $url_parsed['path']; // "pages/users/add/"
And then a simple regex* to parse $url_parsed['host'] for subdomains:
$subdomain = preg_match("/(?:(.+)\.)?[^\.]+\.[^\.]+/i", $url_parsed['host');
// yields array("company.website.com", "company")
* I tested the regex in JavaScript, so you may need to tweak it a little.
Or to avoid the regex:
$sections = explode('.', $url_parsed["host"]);
$subdomain = $sections[0];