My project Url is like this in yii:
http://localhost/php_pro_106/activate/Test?JJ=HEY+OOO
In my action :
public function actionTest()
{
print_r($_GET);
}
I am getting:
Array ( [JJ] => HEY OOO )
But I should get:
Array ( [JJ] => HEY+OOO )
What should I do for this,I need same Url ?
A + in a URL means a space. So you get the result you should have.
If you want to encode a + character in a URL, then you should represent it as %2B.
http://localhost/php_pro_106/activate/Test?JJ=HEY%2BOOO
If you are generating the URL programatically with PHP, you can use the urlencode function.
$keyname = "JJ";
$data = "HEY+OOO";
$url = "http://localhost/php_pro_106/activate/Test?" . urlencode($keyname) . "=" . urlencode($data);
You need to encode your URL (e.g. with urlencode in php). + needs to become %2B.
http://localhost/php_pro_106/activate/Test?JJ=HEY%2BOOO
"+" is one of reserved characters, according to RFC 2396.
If you want "+" in your example, you should use url encoding.
http://localhost/php_pro_106/activate/Test?JJ=HEY%2BOOO
If you produce URL from code, use urlencode() function to generate your URLS.
Related
I would like to remove a querystring parameter from a url that may contain multiple. I have succeeded in doing this using str_replace so far like this:
$area_querystring = urlencode($_GET['area']);
str_replace('area=' . $area_querystring, '', $url);
preg_replace also works, like this:
preg_replace('~(\?|&)area=[^&]*~', '$1', $url)
Either works fine for most URLs. For example:
http://localhost:8000/country/tw/?area=Taipei%2C+Taiwan&fruit=bananas
Becomes:
http://localhost:8000/country/tw/?&fruit=bananas
However, if the querystring contains an apostrophe html entity, nothing happens at all. E.g.:
http://localhost:8000/country/cn/?area=Shanghai%2C+People%27s+Republic+of+China&fruit=bananas
The %27 part of the url (an apostrophe) seems to be the cause.
To be clear, I don't wish to remove all of the URL after the last /, just the area querystring portion (the fruit=bananas part of the url should remain). Also, the area parameter does not always appear in the same place in the URL, sometimes it may appear after other querystring parameters e.g.
http://localhost:8000/country/tw/?lang=taiwanese&area=Taipei%2C+Taiwan&fruit=bananas
You can use the GET array and filter out the area key. Then rebuild the url with http_build_query. Like this:
$url = 'http://localhost:8000/country/cn/?area=Shanghai%2C+People%27s+Republic+of+China&fruit=bananas';
$filtered = array_filter($_GET, function ($key) {
return $key !== 'area';
}, ARRAY_FILTER_USE_KEY);
$parsed = parse_url($url);
$query = http_build_query($filtered);
$result = $parsed['scheme'] . "://" . $parsed['host'] . $parsed['path'] . "?" . $query;
You probably don't need urlencode() -- I'm guessing your $url variable is not yet encoded, so if there are any characters like an apostrophe, there won't be a match.
So:
$area_querystring = $_GET['area'];
should do the trick! No URL encoding/decoding needed.
I have a path alias like below
$node_alias = '/node/add/page?id=1';
what i need is have to append query parameter to a destination url with above url,i have tried below but doenn't work
$node_alias = '/node/add/page?id=1&destination=/admin/content?id=1
Any solution?
You need to url-encode each component in the query string.
The first parameter id=1 is fine, but the destination parameter contains special characters used for delimiting uri components : /, ?, =.
You can use urlencode() or rawurlencode() :
$node_alias = '/node/add/page?id=1&destination=' . rawurlencode('/admin/content?id=1')
There is a drupal way of doing this, using drupal_http_build_query() (d7):
$path = current_path();
$params = drupal_get_query_parameters() + array('destination' => '/admin/content?id=1');
$query = drupal_http_build_query($params);
$node_alias = $path . '?' . $query;
See also urlencode vs rawurlencode?
I am trying to load a single post with custom URL. But it is redirecting to default URL because of "&" character. without & character, it is working fine. But how can I read this "&" in URL? My code is below,
function mutually_add_query_vars($vars) {
return array('L') + $vars;
}
add_filter('query_vars', 'mutually_add_query_vars');
and Loading for a single template
function mutually_template4($template) {
global $wp;
if ($wp->query_vars['L']=='6'.html_entity_decode('&').'au=2') {
return dirname( __FILE__ ) . '/single-ad.php';
}
else {
return $template;
}
}
add_filter('single_template', 'mutually_template4');
I tried with html_entity_decode, but it works nothing.
You need to encode
So & will be %26
What you need to do is URL encoding instead of HTML encoding (Both are different). Therefore, you can replace & with %26. Click here to see reference to URL encoding.
You can also use urlencode ( string $str ) PHP function to encode a string with URL friendly values.
GET parameter pairs in a URL start with a ? then are separated by an & symbol and key/values are separated by an = so myurl.com?L=6&au=2 would be parsed into two separate $wp_query_vars elements one being assigned to L and the other to au there should be no need to manually parse out the & symbol if for some reason your key/value included an & it would need to be URI encoded as %26 as other answers have suggested :)
function mutually_template4($template) {
global $wp;
if ($wp->query_vars['L']=='6' && $wp->query_vars['au']=='2') {
return dirname( __FILE__ ) . '/single-ad.php';
}
else {
return $template;
}
}
add_filter('single_template', 'mutually_template4');
there is an external page, that passes a URL using a param value, in the querystring. to my page.
eg: page.php?URL=http://www.domain2.com?foo=bar
i tried saving the param using
$url = $_GET['url']
the problem is the reffering page does not send it encoded. and therefore it recognizes anything trailing the "&" as the beginning of a new param.
i need a way to parse the url in a way that anything trailing the second "?" is part or the passed url and not the acctual querystring.
Get the full querystring and then take out the 'URL=' part of it
$name = http_build_query($_GET);
$name = substr($name, strlen('URL='));
Antonio's answer is probably best. A less elegant way would also work:
$url = $_GET['url'];
$keys = array_keys($_GET);
$i=1;
foreach($_GET as $value) {
$url .= '&'.$keys[$i].'='.$value;
$i++;
}
echo $url;
Something like this might help:
// The full request
$request_full = $_SERVER["REQUEST_URI"];
// Position of the first "?" inside $request_full
$pos_question_mark = strpos($request_full, '?');
// Position of the query itself
$pos_query = $pos_question_mark + 1;
// Extract the malformed query from $request_full
$request_query = substr($request_full, $pos_query);
// Look for patterns that might corrupt the query
if (preg_match('/([^=]+[=])([^\&]+)([\&]+.+)?/', $request_query, $matches)) {
// If a match is found...
if (isset($_GET[$matches[1]])) {
// ... get rid of the original match...
unset($_GET[$matches[1]]);
// ... and replace it with a URL encoded version.
$_GET[$matches[1]] = urlencode($matches[2]);
}
}
As you have hinted in your question, the encoding of the URL you get is not as you want it: a & will mark a new argument for the current URL, not the one in the url parameter. If the URL were encoded correctly, the & would have been escaped as %26.
But, OK, given that you know for sure that everything following url= is not escaped and should be part of that parameter's value, you could do this:
$url = preg_replace("/^.*?([?&]url=(.*?))?$/i", "$2", $_SERVER["REQUEST_URI"]);
So if for example the current URL is:
http://www.myhost.com/page.php?a=1&URL=http://www.domain2.com?foo=bar&test=12
Then the returned value is:
http://www.domain2.com?foo=bar&test=12
See it running on eval.in.
I want to encode like what browser did,
For example. https://www.google.com.hk/search?q=%2520你好
Should be encoded as https://www.google.com.hk/search?q=%2520%E4%BD%A0%E5%A5%BD
I am using the following regex to encode URI without like rawurlencode encode ~!##$&*()=:/,;?+'
$url = 'https://www.google.com.hk/search?q=%2520你好';
echo preg_replace_callback("{[^0-9a-z_.!~*'();,/?:#&=+$#]}i", function ($m) {
return sprintf('%%%02X', ord($m[0]));
}, $url);
But this will return https://www.google.com.hk/search?q=%252520%E4%BD%A0%E5%A5%BD which has an extra 25.
How can I correctly encode the URL that user inputed without modifying original address?
You can simply use urlencode for this purpose. It will encode %2520你好 into %252520%E4%BD%A0%E5%A5%BD . Use the code below.
<?php
$url = 'https://www.google.com.hk/search?q=%2520'.urlencode("你好").'';
echo $url;
?>
I think this will give you your desired url