On a page of mine, I have a GET as a URL of a website.
mypage.com/page.php?=URLHERE
On this URL, I need the ID at the very end of the URL
mypage.com/page.php?url=http://www.otherwebsite.com/something.php?id=%%%%%%%
These numbers are sometimes different amount of digits, so how would I do that?
My code:
$url = $_GET['url'];
Assuming the url parameter is a properly encoded URL, then useparse_url() to get the URL components and parse_str() to retrieve the id parameter from its query string.
$url = $_GET['url'];
// First parse_url() breaks the original URL up
$parts = parse_url($url);
// parse_str() parses the query string from the embedded URL
$query = parse_str($parts['query']);
// Finally, you have your full id parameter
$id = $query['id'];
assuming the the url has id at the begining of query_string
<?php
$url = $_GET['url'];
$url = basename($url);
$url =explode("?",$url);
$url = explode("=",$url[1]);
echo $url[1];
?>
try the parse_url
$url = parse_url($_GET['url'], PHP_URL_QUERY);
$query = explode('=', $url);
$id = $query[1];
I'd use the PHP function explode() http://php.net/manual/en/function.explode.php
For this example
$numbers = explode("=",$url);
$id_value = $numbers[2];
print $id_value;
Related
i am passing a url as a param to the next page. ?url=http://domain.com i would like to set additional param to a querystring or the url. but only if a specific domain exists in the querystring.
i tried
$url = preg_replace('{http://www.domain.com}','http://www.domain.com?foo=bar/',$_GET['url']);
but this is not working when there is a file name or other params.
any help is appreciated.
This will do what you want:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // the URL you want to inject the parameters into
$params = "new=yes!"; // the new parameters you want to add at the beginning
if (strpos($url, "?") !== false) {
list($url, $b) = explode("?", $url, 2);
$params = "$params&$b";
}
$url .= "?".$params;
Output: http://example.com/example.php?new=yes!&a=b&c=no
If you want to place some parameter to the beginning of the query string you can use parse_url function:
$url = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$parsed_url = parse_url($url);
$new_url = $parsed_url['path'] ."?foo=bar" . ((isset($parsed_url['query']))? urlencode("&").$parsed_url['query'] : "");
var_dump($new_url);
// the output: string(45) "http://www.domain.com?foo=bar%26param=value"
So i worked in a project that i need to encode my URL, for example in my URL i have :
$url = 'http://www.test.com/?sms=+212&text=bla bla'
urlencode($url);
$url = urlencode($url);
Also i tried rawurlencode($url); and i got the same Url without changes !!
so please if someone has any idea i will be so appreciative :)
$url = "http://www.test.com/?sms=+212&text=bla bla";
echo $url = urlencode($url);
urlencode returns a new encoded url, doesn't change the $url passing to it. try this
$encoded_url = urlencode($url);
$encoded_url is what you want
Ur url data should be string.
<?php
$url = 'http://www.test.com/?sms=+212&text=bla bla';
$encoded = urlencode($url);
echo(urlencode($url)); //simple urlencode
echo('<br>');
echo($encoded);//output with varible
echo('<br>');
$url = urlencode($url);
echo($url);
Be attentive in what place u redefined ur url var. Output of that code looks like:
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
http%3A%2F%2Fwww.test.com%2F%3Fsms%3D%2B212%26text%3Dbla+bla
The second one really works:
$url = "http://www.test.com/?sms=+212&text=bla bla";
$url = urlencode($url);
echo $url;
See a demonstration.
I also noticed that
$url = http://www.test.com/?sms=+212&text=bla bla
is an invalid string format. Use ' ' or " " for strings:
$url = "http://www.test.com/?sms=+212&text=bla bla";
I wanted to remove all occurrences of specific pattern of a parameter from a URL using preg_expression. Also removing the last "&" if exist
The pattern looks like: make=xy ("make" is fixed; "xy" can be any two letters)
Example:
http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&
After processing preg_replace, the outcome should be:
http://example.com/index.php?c=y&do=ms&r=k&p=7
I tried using:
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?lang=..&?)/i', '', $url);
However, this did not work well because I have duplicates of make=xx in the URL (which is a case that could happen in my app).
You don't need RegEx for this:
$url = "http://example.com/index.php?ok=no&make=ae&make=as&something=no&make=gr&";
list($file, $parameters) = explode('?', $url);
parse_str($parameters, $output);
unset($output['make']); // remove the make parameter
$result = $file . '?' . http_build_query($output); // Rebuild the url
echo $result; // http://example.com/index.php?ok=no&something=no
You could try using:
$str = parse_url($url, PHP_URL_QUERY);
$query = array();
parse_str($str, $query);
var_dump($query);
This will return to you the query as an array. You could then use http_build_query() function to restore the array in a query string.
But if you want to use regexp:
$url = "index.php?make=ae&ok=no&make=ae&make=as&something=no&make=gr";
echo $url."\n";
$url = preg_replace('/\b([&|&]{0,1}make=[^&]*)\b/i','',$url);
$url = str_replace('?&','?',$url);
echo $url;
This will remove all make in the URL
with rtrim you can remove last &
$url = rtrim("http://example.com/index.php?c=y&make=yu&do=ms&r=k&p=7&","&");
$url = preg_replace('~&make=([a-z\-]*)~si', '', $url);
$url = "index.php?ok=no&make=ae&make=as&something=no&make=gr";
$url = preg_replace('/(&?make=[a-z]{2})/i', '', $url);
echo $url;
Just by using preg_replace
$x = "http://example.com/index.php?c1=y&make=yu&do1=ms&r1=k&p1=7&";
$x = preg_replace(['/(\?make=[a-z]*[&])/i', '/(\&make=[a-z]*[^(&*)])/i', '/&(?!\w)/i'], ['?','',''], $x);
echo $x;
And the result is: http://example.com/index.php?c1=y&do1=ms&r1=k&p1=7
Hope this will be helpful to you guys.
hello i would like to know how to avoid double postings in a request_uri.
as an example:
http://www.example.com/foo/foo/...
should only be:
http://www.example.com/foo/...
for example i would like to create a check that i need for a header-function that needs to be checked before it headers. it should not header if the url-string contains double postings. so the check would be something like:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
if (isset($_SESSION['a'])){
$var = $_SESSION['a'];
if($url !== $urlUnique){
header ('Location:'.$host.'/'.$var.'/'.$basename);
exit;
}
}
if there is someone who could help me out i really would appreciate.
thanks alot.
Try the below code, use explode and get unique and implode it into URL....
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$urlArrayUnique = array_unique($urlArray);
$urlUnique = implode('/', $urlArrayUnique);
You can get dulicate values in $urlArray too...
You can modify this below code to suit as per your need:
$host = $_SERVER['HTTP_HOST'];
$url = $_SERVER['REQUEST_URI'];
$urlArray = explode('/', $url);
$lai = max(array_keys($urlArray));//here we get the max array index
if($urlArray[$lai-1] != $urlArray[$lai])header('Location:'.$host.'/'.$var.'/'.$basename);
Currently I am using parse_url, however the host item of the array also includes the 'WWW' part which I do not want. How would I go about removing this?
$parse = parse_url($url);
print_r($parse);
$url = $parse['host'] . $parse['path'];
echo $url;
$url = preg_replace('#^www\.(.+\.)#i', '$1', $parse['host']) . $parse['path'];
This won't remove the www in www.com, but www.www.com results in www.com.
preg_replace('#^(http(s)?://)?w{3}\.#', '$1', $url);
if you don't need a protocol prefix, leave the second parameter empty
$url = preg_replace('/^www\./i', '', $parse['host']) . $parse['path'];