Jump to content

Trouble understanding htmlspecialchars to change '&' to '&' in a variable


Recommended Posts

I'm passing a variable from one page to the other using $_GET.  For example the variable is 'This Thing & That Thing'.  Echoing the the variable there is only 'This Thing'.  The '& That Thing' is lost.

 

I understand this is happening from because the ampersand is an HTML code and needs to be replaced with '&' .

 

The code I have is

 

$this = $_GET ['this'];

$thisCleaned = htmlspecialchars($this);

echo $thisCleaned;

 

But it is still loosing the string after the ampersand.  I must not be defining the htmlspecialchars correctly. 

yes the variable is passing just fine, it's not in a form but a hyperlink with different variables.

It's the ampersand in the variable name and the loss of the second part of the variable after the ampersand I'm having trouble understanding.

yes the variable is passing just fine, it's not in a form but a hyperlink with different variables.

It's the ampersand in the variable name and the loss of the second part of the variable after the ampersand I'm having trouble understanding.

 

Ah, this has to do with what makes a GET request, specifically the symbols used in the URI.

 

First off, when we var_dump $_GET with the aforementioned string, we get:

array(2) { ["tmp"]=> string(11) "This Thing " ["That_Thing"]=> string(0) "" }

 

The URI I used was /?tmp=This Thing & That Thing.

Or, in other words... /?key1=value&key2.

The ampersand is a GET variable seperator. The first GET var is designated by the question mark symbol, but all GET variables following that are separated by the ampersand.

 

The Solution

An ampersand intended for visual purposes is escaped in a URL as %26.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.