使用 rawurlencode/rawurldecode 编码/解码文本区域值
当使用 PHP 的 rawurlencode
函数将 textarea 的换行符编码为 %0D%0A
存储到 mysql 时,将换行符编码为 %0D%0A
。
例如: 用户输入的 textarea 文本:
a
b
使用 rawurlencode 编码并存储到数据库中会将值存储为 a%0D%0Ab
当从数据库检索并使用 rawurldecode
解码不起作用时,代码给出错误。如何克服这种情况以及存储、检索和显示文本区域值的最佳方法是什么。
When encoding newline of textarea before storing into mysql using PHP with rawurlencode
function encodes newline as %0D%0A
.
For Example:
textarea text entered by user:
a
b
encoding using rawurlencode and store into database will store value as a%0D%0Ab
When retrieving from database and decoding using rawurldecode
does not work and code gives error. How to overcome this situation and what is the best way to store and retrieve and display textarea values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果上述方法对您不起作用,您可以先使用
base64_encode
对此文本区域字符串进行编码,然后对其执行base64_decode
吗?如果文本区域不包含 URL,您应该使用
base64_encode
,然后使用rawurlencode
,然后正常存储。can you first encode this textarea string using
base64_encode
and then perform abase64_decode
on the same, if the above does not work for you.If the textarea does not contain URLs, you should rather use
base64_encode
thenrawurlencode
and then store as normal.您根本不应该使用 rawurlencode 转义数据库数据。
每种目标格式都有自己的转义方法,一般而言,该方法可确保将其从一个地方安全地存储/显示/传输到另一个地方,并且不需要在另一端进行解码。
例如:
您应该注意到,解码这些事情通常是通过浏览器/数据库。所以实际上没有数据被存储转义。并且解码不需要由您的代码完成。
问题可能是因为您使用 rawurlencode 转义了序列,但您的数据库需要特定品牌数据库的转义格式。并使用这个假设来转义它,这是错误的,这弄乱了你的字符串。
结论:找出您正在使用的品牌数据库,查找该数据库的特定转义函数,并对您的所有内容“传输”使用正确的转义函数。
PS:有些定义可能不正确,请评论。我想让这个想法坚持下来,但我可能没有使用所有正确的术语。
You simply should not use rawurlencode for escaping data for your database.
Each target format has it's own escaping method which in general terms makes sure it is stored/display/transferred safely from one place to another, and it doesn't need decoding at the other end.
For instance:
You should notice that decoding these things is often done by the browser/database. So no data is actually stored escaped. And decoding doesn't need te be done by your code.
The problem is probably because you escape a sequence with rawurlencode, but your database expected the escaped format for the specific brand of database. And de-escaped it using that assumption, which was wrong, which messed up your string.
Conclusion: find out what brand database you are using, look up the specific escape function for that database, and use the proper escaping function on all your content "transferral".
P.S.: some definition may not be correct, please comment on that. I wanted to make the idea stick but am probably not using all the right terms.
首先,通过 urlencode() 运行 textarea 的情况很少见
urlencode 不是为此目的而设计的。
其次,如果你仍然想这样做,那么问题可能出在数据库上。首先,您需要告诉我们您使用什么数据库以及您使用什么类型来存储此数据:您将其存储为文本数据还是二进制数据?您在数据库中设置了正确的字符集吗?
First of all it is very uncommon to run textarea through urlencode()
urlencode was not designed for this purpose.
Second, if you still want to do this, then maybe the problem comes from database. First you need to tell us what database you using and what TYPE you using for storing this data: do you store it as TEXT or as BINARY data? Have you setup the correct charset in database?