if we have
$url = "http://subdomain.domin.ext/somepath/file.php";
how to get the domain.ext
when I used
$parse = parse_url($url);
$domin = $parse[host];
then
echo "$domain"; //this returns subdomain.domin.ext
is there any idea to get domain.ext?
$url = "http://subdomain.domin.ext/somepath/file.php";
$parse = parse_url($url);
$domain = $parse['host'];
$lastDot = strrpos($domain,'.');
$ext = substr($domain,$lastDot+1);
This will get you "ext" (e.g. "com" or "gov"). In the case of something like amazon.co.uk, it will give you "uk". If you need "co.uk", you will need to add logic to detect certain TLDs (e.g. "uk") and go to the second dot on those cases.
<?php
$url = "http://subdomain.domain.ext/somepath/file.php";
$parse = parse_url($url);
$domain = substr(strrchr($parse['host'], '.'), 1);
$domain = substr(strrchr(substr($parse['host'], 0, -strlen($domain)), '.'), 1). '.'. $domain;
if(in_array(strstr($domain, '.', true), array('co', 'com', 'net', 'org', 'edu', 'gov', 'mil')))
{
$domain = substr(strrchr(substr($parse['host'], 0, -strlen($domain)), '.'), 1). '.'. $domain;
}
?>
If you have a specific domain with different TLDs, like mysite with .co.uk and .com, than you can basically do the following:
$url = 'http://mysite.co.uk/some-dir/';
$domain_pos = strpos($url, 'mysite');
$domain = substr($url, $domain_pos);
$ext_pos = strpos($domain, '/');
if($ext_pos !== false) { // extra check for paths/sub-dirs
$domain = substr($domain, 0, $ext_pos);
}
echo $domain;
Returns mysite.co.uk or whatever TLD that gets assigned to the $url.
Note the extra check for paths and sub directories. :)
Related
I have a problem to resolve. The first url allow me to display information or not on the website. It work fine.
I identify Account&Edit for example and make action.
http://localhost/boutique/index.php?Account&Edit
$_SERVER['QUERY_STRING'] = Account&Edit
If I rewrite the url, the system does not work and Account&Edit become Account/Edit
http://localhost/boutique/index.php/Account/Edit
$_SERVER['QUERY_STRING'] = '';
How to resolve this element when this element Account&Edit change ?
$_SERVER['QUERY_STRING'] is empty is this case.
Thank you.
I make that to solve the problem.
public function getUrlwithoutSEFU() {
if (empty($_SERVER['QUERY_STRING'])) {
$url = $_SERVER['REQUEST_URI'];
$replace = str_replace('index.php', '', $url);
$replace = str_replace(CLICSHOPPING::getConfig('http_path'), '', $replace);
$replace = substr($replace, 1);
$replace = str_replace('/', '&', $replace);
$url_string = $replace;
} else {
$url_string = $_SERVER['QUERY_STRING'];
}
return $url_string;
}
Take a look at RFC3875: The Common Gateway Interface (CGI) Version 1.1
When you have no ? i nthe request URL, then there's no query string.
Looking at REQUEST_URI would make sense.
This is how request urls are split into environment vars:
# QUERY_STRING
# ↓↓↓↓↓↓↓↓↓↓↓↓
http://localhost/boutique/index.php?Account&Edit
Or the part after the (directly invoked) php script. (This may vary depending on SAPI or be impacted by rewrite rules).
# PATH_INFO
# ↓↓↓↓↓↓↓↓↓↓↓↓↓
http://localhost/boutique/index.php/Account/Edit
Or the full path:
# REQUEST_URI
# ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
http://localhost/boutique/index.php/Account/Edit
Notably you would have to split it up yourself then. Also may show up as REDIRECT_REQUEST_URI for example in the $_SERVER array with some setups.
Solution
public function getUrlwithoutSEFU() {
if (empty($_SERVER['QUERY_STRING'])) {
$url = $_SERVER['REQUEST_URI'];
$replace = str_replace('index.php', '', $url);
$replace = str_replace(CLICSHOPPING::getConfig('http_path'), '', $replace);
$replace = substr($replace, 1);
$replace = str_replace('/', '&', $replace);
$url_string = $replace;
} else {
$url_string = $_SERVER['QUERY_STRING'];
}
return $url_string;
}
I am entering URL's into my database and i was getting all possible entries
I have the following code that takes the http:// or http or www .com .co.uk away
but the problem is this
when I enter a site like hat.com its taking the 'h' away this happens with t, p, w, and if its .co.uk it only removes the .uk
$new = rtrim($url, "/");
$reverse = strrev( $new );
$new = rtrim($reverse, ".www");
$new = rtrim($reverse, "//:ptth");
$new = rtrim($reverse, ".www//:ptth");
$new = rtrim($reverse, "//:sptth");
$new = rtrim($reverse, ".www//:sptth");
$url = strrev( $new );
Whats have I missed and what would I have to add?
Using a regular expression will help here:
preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
The regular expression used will match:
http, https, http://, https://, http://www., https://www. at the beginning of the string
.com, .co.uk at the end of the string.
See this example:
php> $url = 'https://www.example.com';
'https://www.example.com'
php> preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
'example'
php> $url = 'http://hat.com';
'http://hat.com'
php> preg_replace('~(^https?(://(www\.)?)?|\.com$|\.co\.uk$)~', '', $url);
'hat'
I'm using this snippet for reading images on different websites:
$image = new Imagick('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');
But sometimes, I get an error like this (about 20% of the time):
ImagickException
Unable to read the file: http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]
Imagick->__construct()
The error seems to be consistent through this whole domain, but sometimes it's different from image to image on the same domain.
Questions
Why is this a problem?
How can we fix it?
Is there an alternative solution?
STEP 1 : GET URL
$url = 'http://blablabla.com/blabla.jpeg';
STEP 2 : Save blog content to a variable
$image = file_get_contents($url);
STEP 3 : Create Imagick object
$img = new Imagick();
STEP 4 : Instruct imagick object to read blob content of the image
$img -> readImageBlob($image);
STEP 5 : Save (or do operations) on image
$img -> writeImage('/var/www/html/uploads/img.jpg');
STEP 6 : Destroy imagick instance
$img -> destroy();
So I figured out I needed to encode the url properly. I'm not sure if this code is optimal, but it works.. and could hopefully help someone else.
$parsedUrl = parse_url('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');
$info = pathinfo($parsedUrl['path']);
$dirname = explode('/', $info['dirname'] ?: '');
$dirname = array_filter($dirname, 'strlen');
$dirname = array_map('urlencode', $dirname);
$dirname = implode('/', $dirname);
$basename = urlencode($info['basename'] ?: '');
$path = array_filter(array($dirname, $basename), 'strlen');
$path = '/' . implode('/', $path);
$query = explode('&', $parsedUrl['query'] ?: '');
foreach ($query as &$set)
{
$set = explode('=', $set, 2);
$set = array_map('urlencode', $set);
$set = implode('=', $set);
}
$query = implode('&', $query);
$uri = array_filter(array($path, $query), 'strlen');
$uri = implode('?', $uri);
$fragment = urlencode($info['fragment'] ?: '');
$uri = array_filter(array($uri, $fragment), 'strlen');
$uri = implode('#', $uri);
$scheme = $parsedUrl['scheme'] ?: '';
$host = $parsedUrl['host'] ?: '';
$url = array_filter(array($scheme, $host), 'strlen');
$url = implode('://', $url);
$url .= $uri;
$image = new Imagick($url);
OBS!
This code will leave notifications.
Try to use urlencode function for encode special chars of url:
$image = new Imagick(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));
Or if not work, try this:
$content = file_get_contents(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));
$image = new Imagick($content);
I just encountered a similar issue. I was using file_get_contents() and was getting the same "ImagickException The filename is too long". The fix for me was finding the tmp directory and setting the correct permissions for it.
www.foo.com/Base/index.php/controller/method/
This is my url, I need to get only "controller/method" part out of it - is there any elegant way to achieve this?
parse_url($url, PHP_URL_PATH)
This would be great, but it returns the whole "Base/index.php/controller/method" part.
$url = "http://www.foo.com/Base/index.php/controller/method/";
$path = parse_url($url, PHP_URL_PATH);
$parts = explode("/", $path);
$index = array_search("index.php", $parts);
$controller = $parts[$index+1];
$method = $parts[$index+2];
You did not provide any additional information about url format. But as example, you may use regular expressions with preg_match():
$url = "http://www.foo.com/Base/index.php/controller/method/";
preg_match ("/^.*Base\/index.php\/([a-zA-Z0-9_]*)\/([a-zA-Z0-9_]*)\/?/", $url, $data);
$controller = $data[1];
$method = $data[2];
I need to get the last string content of the url between / and /
For example:
http://mydomain.com/get_this/
or
http://mydomain.com/lists/get_this/
I need to get where get_this is in the url.
trim() removes the trailing slash, strrpos() finds the last occurrence of / (after it's trimmed), and substr() gets all content after the last occurrence of /.
$url = trim($url, '/');
echo substr($url, strrpos($url, '/')+1);
View output
Even better, you can just use basename(), like hakre suggested:
echo basename($url);
View output
Assuming there always is a trailing slash:
$parts = explode('/', $url);
$get_this = $parts[count($parts)-2]; // -2 since there will be an empty array element due to the trailing slash
If not:
$url = trim($url, '/'); // If there is a trailing slash in this URL instance get rid of it so we're always sure the last part is where we expect it
$parts = explode('/', $url);
$get_this = $parts[count($parts)-1];
Something like this should work.
<?php
$subject = "http://mydomain.com/lists/get_this/";
$pattern = '/\/([^\/]*)\/$/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>
Just use parse_url() and explode():
<?php
$url = "http://mydomain.com/lists/get_this/";
$path = parse_url($url, PHP_URL_PATH);
$path_array = array_filter(explode('/', $path));
$last_path = $path_array[count($path_array) - 1];
echo $last_path;
?>
You can try this:
preg_match("/http:\/\/([a-z0-9\.]+)\/(.+)\/(.*)\/?/", $url, $matches);
print_r($matches);