Get part of the current url PHP [closed] - php

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];

Related

How to check specific word in the url? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Suppose I have 3 different urls. All are illustrated below.
1. http://www.example.com/foo/bar/index.php?id=1
2. http://www.example.com/bar/foo/index.php?id=1
3. http://www.example.com/foo/contact.php
How can I check whether the given url contains foo as of first param.
I mean how can I get only 1st and 3rd url as true and 2nd as false.
Method 1: Use parse_url():
$array = parse_url($_SERVER['REQUEST_URI']);
$path = explode('/', $array['path']);
if ($path[1]==='foo')
{
// Here ya go! :-)
}
Method 2: Use a Regular Expression:
if (preg_match('/^http:\/\/www\.example\.com\/foo\/.*$/', $_SERVER['REQUEST_URI'])
{
// Here ya go! :-)
}
https://regex101.com/r/jZ0pL1/1
To get your url parameters first we will need to parse your url
$parsedUrl = parse_url($_SERVER['REQUEST_URI']);
Now we will split it into parts
$params = explode('/', $parsedUrl['path']);
And finally, to get the first parameter
// Getting the second key => value pair
$firstParam = $params[1];
To view all parameters
print_r($params);
You can use parse_url() as below,
$arr = parse_url($url);
You will get $arr[2]= 'foo', check the requested url using this and that is what you want...

How to grab data from an external URL [closed]

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
?>

About find domain name [closed]

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.

How do i extract a number from a url using PHP [closed]

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);
}

Get the last part of the URI [closed]

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;

Categories