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
Related
I am writing some code and when I echo this code
echo json_encode($_SERVER);
I get this output
{
"HTTP_CONTENT_TYPE":"application\/x-www-form-urlencoded",
"HTTP_USER_AGENT":"WordPress\/5.7.2; https:\/\/dvsucb.local",
"REQUEST_TIME_FLOAT":1625414926.336319,
"REQUEST_TIME":1625414926
}
I want to extract the URL inside HTTP_USER_AGENT that will be: https://dvscucb.local
I want to extract only the URL not the WordPress version. How to get this ?
You don't need to use json_encode() here.
You can access the HTTP_USER_AGENT property from the $_SERVER array, and then use a regular expression to extract the URL from the string.
Here is a fairly simple example:
$userAgent = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/WordPress\/[\d]+.[\d]+.[\d]+;\s(.*)/", $userAgent, $matches)) {
echo $matches[1];
}
i tried a long time to decode a ü to %FC but nothing helped. I hope someone can help me.
Here is a example url with this code:
http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M%FCnchen&noList=false
Best regards
Gimo
Here You can encode your URL like this way :
<?php
// Here to encode whole URL
$url = "http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=München&noList=false";
$encoded = urlencode($url);
echo "Encoded URL : ".$encoded."</br></br></br>";
$decoded = urldecode($encoded);
echo "Decoded URL : ".$decoded."</br></br></br>";
//Or If you want to just encode the certain required part so here you can do like this way
$url = "http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M".urlencode("ü")."nchen&noList=false";
echo "URL With Required Encoded Part : ".$url;
?>
OUTPUT :
Encoded URL : http%3A%2F%2Fwww2.dasoertliche.de%2F%3Fform_name%3Dsearch_nat_ext%26vert_ok%3D1%26zvo_ok%3D1%26kgs%3D09162000%26rgid%3D%26buab%3D%26zbuab%3D%26buc%3D2247%26sst%3Dyes%26page%3D1%26action%3D43%26st%3DDrygalski-Allee%2B51%26choose%3Dtrue%26sfn%3Dyes%26image%3DFinden%26skw%3Dyes%26context%3D1%26book%3D96%26ci%3DM%C3%BCnchen%26noList%3Dfalse
Decoded URL : http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=München&noList=false
URL With Required Encoded Part : http://www2.dasoertliche.de/?form_name=search_nat_ext&vert_ok=1&zvo_ok=1&kgs=09162000&rgid=&buab=&zbuab=&buc=2247&sst=yes&page=1&action=43&st=Drygalski-Allee+51&choose=true&sfn=yes&image=Finden&skw=yes&context=1&book=96&ci=M%C3%BCnchen&noList=false
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.
So, I have dynamically generated pages that follows the following format:
http://example.com/name/first_name/last_name/John%2C+Smith
What is the php code to output the last part of the url?
so, it becomes like "John, Smith".
Thank you so much.
EDIT:
I realized that the URL is ended with another / and the answers given below does not pick it up. What change should I make?
http://example.com/name/first_name/last_name/John%2C+Smith/
EDIT 2:
So, the link is dynamically generated as the following:
href="http://example.com/name/first_name/last_name/<?php echo $full_name ?>"
Split the url, get the last fragment then URL decode it:
<?
$urlarray=explode("/",$url);
$end=$urlarray[count($urlarray)-1];
$end=urldecode($end);
//go on using $end then
?>
You could do this with a regex.
echo preg_replace_callback('~.*/(.*)~',
function($matches) {
return urldecode($matches[1]);
},
'http://example.com/name/first_name/last_name/John%2C+Smith');
Regex demo: https://regex101.com/r/bE3bO5/1
Output:
John, Smith
Update:
echo preg_replace_callback('~.*/(.+)~',
function($matches) {
return rtrim(urldecode($matches[1]), '/');
},
'http://example.com/name/first_name/last_name/John%2C+Smith/');
You can use parse_url with second parameter PHP_URL_PATH
$url = urldecode("http://example.com/name/first_name/last_name/John%2C+Smith");
$arr = array_filter(explode('/',parse_url($url, PHP_URL_PATH)));
print_r(end($arr));
Edited:
As per requirement for dynamic url you can use
$url = urldecode("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
Looking for how to get the complete string in a URI, after the away?to=
My code:
if (isset($_SERVER[REQUEST_URI])) {
$goto = $_SERVER[REQUEST_URI];
}
if (preg_match("/to=(.+)/", $goto, $goto_url)) {
$link = "<a href='{$goto_url[1]}' target='_blank'>{$goto_url[1]}</a>";
The original link is:
https://domain.com/away?to=http://www.zdf.de/ZDFmediathek#/beitrag/video/2162504/Verschw%C3%B6rung-gegen-die-Freiheit-%281%29
.. but my code is cutting the string after the away?to= to only
http://www.zdf.de/ZDFmediathek
You know the fix for this preg_match function to allow really every character following the away?to= ??
UPDATE:
Found out, that $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING'] is already cutting the original URL. Do you know why and how to prevent that?
try use (.*) to get all after to=
$str = 'away?to=dfkhgkjdshfgkhldsflkgh';
preg_match("/to=(.*)/", $str, $goto_url);
echo $goto_url[1]; //dfkhgkjdshfgkhldsflkgh
Instead of extracting the URL with regex from the request URI you can just get it from the $_GET array:
$link = "<a href='{$_GET['to']}' target='_blank'>{$_GET['to']}</a>";