Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I search in internet and i found this code for find domain name
function get_domain($url)
{
$pieces = parse_url($url);
$domain = isset($pieces['host']) ? $pieces['host'] : '';
if (preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs))
{
return $regs['domain'];
}
return false;
}
Its works for http://www.google.com or http://www.google.co.uk
But its not working for test.web.tv.
Anybody can help me ?
How i can find min domain ?
Thanks
The function parse_url() requires a valid URL. In this case test.web.tv isn't valid, so parse_url() won't give you the expected results. In order to get around this, you could first check if the URL has the http:// prefix, and if it doesn't, manually prepend it. That way, you can get around the limitation of parse_url().
However, I think it'd be better to use the following function.
function getDomain($url)
{
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
$domain = implode('.', array_slice(explode('.', parse_url($url, PHP_URL_HOST)), -2));
return $domain;
}
Explanation:
The given URL's passed to parse_url() with the PHP_URL_HOST flag and the full host is obtained
It's exploded with . as a delimiter
The last two pieces of the array is sliced -- ie. the domain name
It's joined back using implode()
Test:
echo getDomain('test.web.tv');
Output:
web.tv
Demo!
Note: It's a modified version of my own answer here combined with Alix's answer here.
This function currently doesn't work for .co.uk domain extensions -- you can easily add a check and change the array_slice function accordingly.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an application where a url will be generated from website #1, after a user signs up.
(i.e. user will get their own url: http://www.example.com/?affid=12345)
I need to grab the numbers in the above url by using PHP, but I don't understand how this would work. How can I grab the actual numbers from the url above using PHP?
I should also mention that the website where I will generating PHP code is not on the same server as the URL above.
Use parse_url and parse_str for that:
$url = 'http://www.example.com/?affid=12345';
$query = parse_url($url, PHP_URL_QUERY);
parse_str($query, $vars);
echo $vars['affid']; // outputs 12345
To get the number i.e 12345 in this URL http://www.example.com/?affid=12345, use $_GET or $_REQUEST which will give you an associative array. you can use $_GET and $_REQUEST throught the application, while using $_GET OR $_REQUEST you must clean before using in your code.
echo $_GET['affid'];
OR
echo $_REQUEST['affid'];
You can use parse_url function to extract the query
<?php
$url = 'http://www.example.com/?affid=12345';
$parsed_url = parse_url($url);
echo $parsed_url['query'];
//print : affid=12345
?>
After that, you can parse the result with parse_str function
<?php
parse_str($parsed_url['query'],$result);
var_dump($result);
//print array(1) { ["affid"]=> string(5) "12345" }
echo $values[0]
//print 12345
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How i cant get a specific part of the current url? for example, my current url is:
http://something.com/index.php?path=/something1/something2/something3/
Well, i need to print something2 with php.
Thanks!
You use the explode function in PHP to separate the URL by the first parameter (in this case a forward slash). To achieve your goal you could use;
$url = "http://something.com/index.php?path=/something1/something2/something3/";
$parts = explode('/', $url);
$value = $parts[count($parts) - 2];
All these other example seem to focus on your exact example. My guess is that you need a more flexible way of doing this, as the explode-only approach is very fragile if your URL changes and you still need to get data out of path parameter in query string.
I will point out the parse_url() and parse_str() functions to you.
// your URL string
$url = 'http://something.com/index.php?path=/something1/something2/something3/';
// get the query string (which holds your data)
$query_string = parse_url($url, PHP_URL_QUERY);
// load the parameters in the query string into an array
$param_array = array();
parse_str($query_string, $param_array);
// now you can look in the array to deal with whatever parameter you find useful. In this case 'path'
$path = $param_array['path'];
// now $path holds something like '/something1/something2/something3/'
// you can use explode or whatever else you like to get at this value.
$path_parts = explode('/', trim($path, '/'));
// see the value you are interested in
var_dump($path_parts);
You could do something like this:
$url = explode('/', 'http://something.com/index.php?path=/something1/something2/something3/');
echo $url[5];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an SMF website and i'm actually trying to get some header information which includes the title of a particular thread, the url but i've been finding it difficult to get the unique link affixed to the url using PHP.
Here's the url: http://example.com/index.php?topic=6449.msg6858
I'm actually looking for a way to extract the number 6449, I've tried to use the php GET function but it doesn't work.
$parts = explode('.', $_GET['topic']);
echo $parts[0];
// PHP 5.4+
echo explode('.', $_GET['topic'])[0];
See it in action
This would work, too
echo (int) $_GET['topic'];
See it in action
You want to use a combination of substr and strpos (to find the first occurence of a period)
$number = substr($_GET['topic'], 0, strpos($_GET['topic'], '.'));
// 6449
$arr = array();
if (preg_match("/([\\d]+)([.]{1}msg[\\d]+)/", $_GET["topic"], $arr) == 1) {
echo $arr[1];
} else {
trigger_error("not found", E_USER_ERROR);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
if i output my URI i also get the folder in which it is located at.
echo $_SERVER['REQUEST_URI']; //outputs /abcgetoutofjail/admin.php?make_account
im on localhost right now, and the website is under folder in htdocs abcgetoutofjail
i need only admin.php?make_account
i need the last part of the url in any way
how can i achieve that with either another way of getting uri or using a string function to cut SERVER URI
function DescriptionAndTitle($uri)
{
echo $uri;
}
In Php, you can achieve this using explode & count function.
$uri = '/some-dir/yourpage.php?q=bogus&n=10';
$uri = explode('/', $uri);
$len = count($uri);
print $uri[$len-1];
To get the last part of a URL I usually do something like the following:
$url = parse_url($_SERVER['REQUEST_URI']);
$partsofurl = explode("/", $url['path']);
$partyouwant = end($partsofurl);
$needed_part = end(explode('/', $_SERVER['REQUEST_URI']));
I believe this is what you're looking for:
$dir = explode('/', dirname($_SERVER['PHP_SELF']));
$dir = '/'.end($dir);
echo $dir;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i got a url as string i want to replace anything between http:// and first slash / (in this example i want to replace proxy-63.somesite.com with video2.de.secondsite.net). could any one tell me how this can be done ?Thanks
Note: data between http:// and first slash after it is dynamic so i cant just use replace function!
http://proxy-63.somesite.com/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8
replaced with :
http://video2.de.secondsite.net/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8
You can use parse_url
$originalUrl = 'http://yourserver';
$parts = parse_url( $originalUrl);
$newServer = 'video2.de.secondsite.net';
$newUrl = $parts['scheme'] . '//' . $newServer . $parts['path'] . '?' .
$parts['query'] . '#' . $parts['fragment'];
You may have to be more careful and actually test if there is a query and a fragment before blindly adding the # and the ?
And here's a JS solution since I already wrote it down, you don't even have to test for ? and # since they are part of location.hash and location.search
location.protocol + '//' + 'video2.de.secondsite.net' +
location.pathname + location.search + location.hash
The original URL can be different each time, the preg_match will find the root.
$url = 'http://proxy-63.somesite.com/sec(dfgghdfdgd987435392429324343241k)/video/600/500/12345678_mp4_h264_aac_2.m3u8';
preg_match('#^(?:http://)?([^/]+)#i', $url, $domain);
$host = $domain[1];
$newUrl = str_replace($host, 'video2.de.secondsite.net', $url);
echo $newUrl;