I have a URL:
market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001
How can I get this value from above URL 000146132647632302db63d958690001
Can I use preg_match function or something else.
If you are receiving it as real URL than as simple as:
echo $_GET['trackingid'];
Else:
$queryArray = [];
$query = parse_url(
urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001"),
PHP_URL_QUERY
);
parse_str($query, $queryArray);
echo $queryArray['trackingid'];
Live example
You can use regular expressions for that. Otherwise you cann access it direcly with $_GET
$url='market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001';
if(preg_match("/([^\?]*)\?trackingid%(d*)/",$url,$matches)){
echo $matches[1];
} else {
$_GET['trackingid']
}
1) One method is using explode().
$test = "market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001";
$url = explode("trackingid=",urldecode($test));
echo $url[1];
Working Demo : Click Here
2) Another is you can use preg_match() you can achieve it.
3) If you are getting it in url then get it using $_GET['trackingid'].
4) Using parse_str().
$url = urldecode("market://details?id=com.balancehero.truebalance&referrer=utm_source%3Dapp%26utm_medium%3Dlink%26utm_term%3D%26utm_content%3D%26utm_campaign%3Dmgm%26campid%3D2FC42T27%26m%3D1%26trackingid%3D000146132647632302db63d958690001");
parse_str($url, $tempArray);
echo $tempArray['trackingid'];
Related
my php function is
$exp3 = $_GET["url"];
echo $exp3;
This function gets me this link for example
"http://www.streamuj.tv/video/687aa15fe046f21cc1e3"
What do I need is to transform this function to get just the code of the url. In this case its 687aa15fe046f21cc1e3.
Can you please help? Thank you
You can use explode and get the last
$myArray= explode('/',$exp3 );
$my_Last = end($myArray);
echo $my_last;
You can use this:
$code = array_pop(explode('/', $exp3));
echo $code;
This question already has answers here:
Get only filename from url in php without any variable values which exist in the url
(13 answers)
Closed 7 years ago.
I wanted to print the last characters after "/" in a url. but instead it is printing the whole url, I expected the output to be just "index.php" instead it is printing out the whole url.
How should i go about doing it right?
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strpos($data, "/") + 1);
echo $whatIWant;
You can see it here
You should get the actual link by
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$getpath=explode("/",$actual_link);
echo end($getpath);
?>
Short Explanation :
Step 1 : Get the url by
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
Step 2 : Explode with slash
explode("/",$actual_link)
Step 3 : Get the last part
end($getpath);
Try this..
<?php
$data = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$whatIWant = explode("/",$data);
echo end($whatIWant);?>
You can also try it this way using strrchr :
$url = 'http://spiritofethiopia.com/test/test/test/index.php';
$str = substr(strrchr($url, '/'), 1);
echo $str;
strrchr — Find the last occurrence of a character in a string.
You can use preg_match whith a correct RegExp to capture the end of the URL.
<?php
$data = $_SERVER['REQUEST_URI'];
if(preg_match('#/([^/]*?)$#', $data, $matches) == 1) {
echo $matches[1];
}
else {
// Should not happen
/*
* Throw exception
*/
}
?>
Try strripos instead of strpos, it may works
<?php
$data = $_SERVER['REQUEST_URI'];
$whatIWant = substr($data, strripos($data, "/") + 1);
echo $whatIWant;
?>
Try This:
working solution,
<?php
$url = 'http://test/test/test/index.php';
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-1];
?>
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);
?>
I'm trying to change a value in a string that's holding my current URL. I'm trying to get something like
http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory
to look like
http://myurl.com/test/end.php?req=&srclang=english&destlang=english&service=MyMemory
replacing begin.php for end.php.
I need the end.php to be stored in a variable so it can change, but begin.php can be a static string.
I tried this, but it didn't work:
$endURL = 'end.php';
$beginURL = 'begin.php';
$newURL = str_ireplace($beginURL,$endURL,$url);
EDIT:
Also, if I wanted to replace
http://myurl.com/begin.php?req=&srclang=english&destlang=english&service=MyMemory
with
http://newsite.com/end.php?req=&srclang=english&destlang=english&service=MyMemory
then how would I go about doing that?
Assuming that you want to replace the script filename of the url, you can use something like this :
<?php
$endURL = 'end.php';
$url ="http://myurl.com/test/begin.php?req=&srclang=english&destlang=english&service=MyMemory";
$pattern = '/(.+)\/([^?\/]+)\?(.+)/';
$replacement = '${1}/'.$endURL.'?${3}';
$newURL = preg_replace($pattern , $replacement, $url);
echo "url : $url <br>";
echo "newURL : $newURL <br>";
?>
How do you want them to get to end.php from beigin.php? Seems like you can just to a FORM submit to end.php and pass in the variables via POST or GET variables.
The only way to change what page (end.php, begin.php) a user is on is to link them to another page from that page, this requires a page refresh.
I recently made a PHP-file for this, it ended up looking like this:
$vars = $_SERVER["QUERY_STRING"];
$filename = $_SERVER["PHP_SELF"];
$filename = substr($filename, 4);
// for me substr removed 'abc/' in the beginning of the string, you can of course adjust this variable, this is the "end.php"-variable for you.
if (strlen($vars) > 0) $vars = '?' . $vars;
$resultURL = "http://somewhere.com" . $filename . $vars;
I use $_SERVER['QUERY_STRING'] to get the query sting.
A example would be a=123&b=456&c=789
How could I remove the b value from the query string to obtain a=123&c=789 where b can be any value of any length and is alpha numeric.
Any ideas appreciated, thanks.
A solution using url parsing:
parse_str($_SERVER['QUERY_STRING'], $result_array);
unset($result_array['b']);
$_SERVER['QUERY_STRING'] = http_build_query($result_array);
The value is going to be $_GET['b'].
How about:
str_replace('&b='.$_GET['b'], '', $_SERVER['QUERY_STRING']);
you can use this function:
function Remove_QS_Key($url, $key) {
$url = preg_replace('/(?:&|(\?))'.$key.'=[^&]*(?(1)&|)?/i', "$1", $url);
return $url;
}
to remove any key you want, e.g.
echo Remove_QS_Key("http://domain.com/?a=b&ref=dusername&c=d&e=f&g=h", "ref");
result
http://www.domain.com/?a=b&c=d&e=f&g=h
Try this:
$query_new = preg_replace('/(^|&)b=[^&]*/', '', $query);
All the answers look good, but it will be more flexible if you do:
// Make a copy of $_GET to keep the original data
$getCopy = $_GET;
unset($getCopy['b']); // or whatever var you want to take out
// This is your cleaned array
var_dump($getCopy);
// If you need the URL-encoded string, just use http_build_query()
$encodedString = http_build_query($getCopy);
You simply make a variable using $_GET and exclude b query string in build process:
$query_string_new = 'a=' . urlencode($_GET['a']) . '&c=' . urlencode($_GET['c']);
The $query_string_new should now contain a=123&c=789
Pear already has a class(Net_URL2) that handles URL parsing/building:
Install via Composer: https://packagist.org/packages/pear/net_url2
Install as include: https://github.com/pear/Net_URL2/blob/master/Net/URL2.php
Example code:
$url = new Net_URL2('http://www.example.com/?one=1');
$url->setQueryVariable('two', 2);
echo $url; // http://www.example.com/?one=1&two=2
Here is a function to replace a query parameter: (like example.com?a=1&b=2 -> example.com?a=5&b=2)
function replace_qs_key($key, $value) {
$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .
"://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$current_url_without_qs = strtok($current_url, '?');
parse_str($_SERVER['QUERY_STRING'], $query_params);
$query_params['page'] = $value;
$_SERVER['QUERY_STRING'] = http_build_query($query_params);
$new_url = $current_url_without_qs .'?'. $_SERVER['QUERY_STRING'];
return $new_url;
}