URL: example.com/search:#searchWord
Since the # is not send to the server, it is changed to %23.
In the search page I want to display
Results for: #searchWord
But what I get is %23searchWord. I tried htmlspecialchars(), but nothing happens.
To achieve your goal use urldecode.
echo urlencode('example.com/search:#searchWord');
// output: example.com%2Fsearch%3A%23searchWord
echo urldecode('example.com%2Fsearch%3A%23searchWord');
// output: example.com/search:#searchWord
Related
I want to use the GET method to send a string to the receive page, but if the string includes '#', the receiver page can only get the sub string before the '#'.
As the following example:
test
When I click the 'test' link to open the 'test.php' page, which has the following code:
<?php
if(isset($_GET["q"])) {
echo $_GET["q"];
}
?>
It only display 'string1' on the page, '#string2' is missing.
So I want to know what happened to the string, and how to fix this problem.
Thank you for any help!
=======Update===========
With the help of #Eric Shaw and #JP Dupéré, I know how to fix this problem.
The simplest way is encoding the string before using the get method.
To encode the query string, you can:
use urlencode() in PHP, and urldecode() can decode the string.
use encodeURIComponent() in JavaScript, and decodeURIComponent() can decode the string.
Try
urlencode("string1#string2")
before calling GET.
The #foo is used to jump to an <a name="foo"/> tag on the page, rather than viewing the top of the page when the browser loads it.
The stuff after the # is processed by the browser and NOT sent to the server.
You can escape the # and the escaped version will be sent to the server, i.e.
test
will do what you want I think
This escaping is also a common technique to get the # passed along in the URL for redirectors.
Using the following in PHP:
var_dump($obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image)
Results:
string(56) "https://api.site.com/image_link/user-76643221_b691647a"
I only need the link, without those extra parts. Any ideas?
var_dump shows the types and sizes of variables; it's meant for debugging. If you only need the link, then instead of using var_dump use print or echo
echo $obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image;
Results:
https://api.site.com/image_link/user-76643221_b691647a
I have a download page that take arguments like the download URL, the download-counter data file url, and the page to return to after downloading.
It is arranged like so:
start.php?url=...&page=...&file=...
(Download url, redirect page, counter file)
The problem is, when the redirect page contains PHP arguments with ? and & symbols, the URL becomes a confusing mess for PHP to work with.
Example:
start.php?url=URLTEXT&page=page?test1=x&test2=xx&file=FILETEXT
What should happen:
url=URLTEXT
page=page?test1=x&test2=xx
file=FILETEXT
what happens:
url=URLTEXT
page=page?test1=x
test2=xx
file=FILETEXT
How could I substitute characters or somehow make these arguments pass correctly in php?
Thanks for any help you can give.
Well, I'm not sure how your "messed up" URL looks like. However the string after the "?" is called Query String, and you can decode/encode it with
urlencode($normalString); //will be encoded for use in URL
urldeocde($queryString); //will be decoded for "normal" use
EDIT:
Here is some short example:
echo "Encode for use in URL: ";
echo urlencode("this is a string & üäöllasdlk<bbb2");
echo "<br />";
echo "Decode to use it in your script: ";
echo urldecode($_SERVER['QUERY_STRING']);
Output:
Encode for use in URL:
this+is+a+string+%26+%C3%BC%C3%A4%C3%B6llasdlk%3Cbbb2
Decode to use it in your script: test=12
(Assuming you have a Querystring containing the variable test=12)
Just use htmlspecialchars function on your URL string:
http://php.net/manual/en/function.htmlspecialchars.php
For example, if there is a url like www.website.com/hello/richard, would it be possible to echo hello and 100 separately onto my page.
eg:
hello how are you today richard
You can get the data from $_SERVER['REQUEST_URI'] and then do whatever you like with it.
Yes it would be. Try this:
$myURL = $_SERVER['REQUEST_URI'];
$myTokens = explode('/', $myURL);
echo $myTokens[1] . "blah" . $myTokens[2];
This code gets the current URL into the myURL variable, then it calls a function called explode which turns it into an array based on the position of the '\' character. Then it echos out certain elements of that array. If you play around with output using echo you will soon see for yourself what is going on.
Sure that's possible. You can get URL as a string using $_SERVER['request_uri]. Then you might want to use explode function to firm array of strings where delimiter is /. Then you may parse it. Or you can do this via .htaccess using rewrite rule
When I output $_SERVER['REQUEST_URI']; on:
http://localhost/tools/?tool=cs&sub=1
I get:
/tools/?tool=cs⊂=1
Is there other solution to get /tools/?tool=cs&sub=1 besides using & instead of & ?
It's because you're echoing it to your browser - &sub is being interpreted as an HTML entity (⊂).
If you echo htmlentities($_SERVER['REQUEST_URI']); you'll get what you expect.
You have to use the right encoding for the environment you're in - in HTML that means using &.
try this
echo urldecode($_SERVER['REQUEST_URI']);
How are you outputting this value? If you're dumping it to the browser, are you sure it's not trying to 'decode' embedded ampersands?
Try a file with just
<?php phpinfo();
and look to see what the value is displayed as (near the bottom)