Cannot get the “+” sign to display
-
21-08-2019 - |
Question
I have this value in a mysql database: "A+" it's the name of a magazine. I can see it sitting in the database, however i cannot manage to have it display via php. When i fetch the sql data, it outputs
"A "
I tried utfencode() utfdecode() htmlentities() ... to no avail. I fail to diagnose the problem. In case it matters, i fetch this data via ajax (jquery load() function)
UPDATE:
It turns out the + sign is removed while parsing the data through a regular expression. can't seem to find a fix for that either. preg_quote() is of no use.
Solution
It is possible that jquery is interpreting it as the url encoded '+' sign, which can be interpreted as a space. Post some code and I'm sure somebody can give you a full answer.
OTHER TIPS
The +
character is interpreted as space when the data is declared as application/x-www-form-urlencoded
:
This is the default content type. Forms submitted with this content type must be encoded as follows:
- Control names and values are escaped. Space characters are replaced by '
+
', and then reserved characters are escaped as described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by '%HH
', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., '%0D%0A
').- The control names/values are listed in the order they appear in the document. The name is separated from the value by '
=
' and name/value pairs are separated from each other by '&
'.
So you have to encode it properly. In JavaScript, use the encodeURIComponent
function if you want to use +
character in an URI:
"http://www.example.com/?q=" + encodeURIComponent("A+")
Do you pass it as an javascript parameter without urlencode()'ing it?
Well, since the only way to make it work is to do
echo urlencode("this is the A+ text");
It works and does not seem to damage the rest of the text, but i don't understand the rationale behind this. Anyway, thanks to all of those who tried to help.
You can add a escape character before the \ and see how that works. If not convert the field data using htmlentities