PHP URL 编码/解码表单字段中的漂亮引号%u2019
由于某种原因,在从文本表单向 php 提交类似 Jack's Spindle
的字符串后,我得到:
Jack%u2019s Spindle
This is not PHP 的 urlencode()
会做什么,即 Jack%92s+Spindle
或 rawurlencode()
= Jack%92s%20Spindle
因此,urldecode()
和原始版本无法解码该字符串。此类字符串还有其他函数吗?
--
此外,Jack’s Spindle
将是对上述内容进行编码的 HTML 安全方式,但 urlencode()
和 raw* 会产生:Jack%26%238217%3Bs+Spindle 和
Jack%26%238217%3Bs%20Spindle
...
%u2019
来自哪里?它代表什么?你怎样才能把它恢复到那个无伤大雅的撇号呢?
For some reason, after submitting a string like this Jack’s Spindle
from a text form to php, I get:
Jack%u2019s Spindle
This is not what PHP's urlencode()
would do, which would be Jack%92s+Spindle
or rawurlencode()
= Jack%92s%20Spindle
Thus, urldecode()
and the raw version don't work to decode that string... Is there another function for such strings?
--
Also, Jack’s Spindle
would be the HTML-safe way to encode the above, but urlencode()
and raw* for that yields: Jack%26%238217%3Bs+Spindle
and Jack%26%238217%3Bs%20Spindle
respectively...
Where is the %u2019
coming from? What does it represent? How do you get it back to just that innoculous apostrophe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,只有你能告诉我们它来自哪里。您从哪里获取文本以及将其提交给哪些转换?我承认我还没有看到那种编码策略。
也就是说,它与 Javascript 编码 UTF-16 代码单元的方式非常相似:
\uXXXX
,其中每个X
代表一个十六进制字符。要将其转换为 HTML 实体,您可以执行以下操作:Well, only you can tell us from where that came from. From are you getting your text and which transformations is it being submitted to? I confess I haven't seen that encoding strategy yet.
That said, it's very similar to the way Javascript encodes UTF-16 code units:
\uXXXX
where eachX
represents a hexadecimal character. To convert it to HTML entities, you could do:preg_replace
函数调用中的正则表达式应该有一个结束分隔符,即:preg_replace('/%u([a-fA-F0-9]{4})/', ' &#x\\1;', $string)
。The Regular Expression in the
preg_replace
function call should have an ending delimiter, i.e:preg_replace('/%u([a-fA-F0-9]{4})/', '&#x\\1;', $string)
.我有这样的问题。
当我通过 javascript/ajax 将变量发送到 php 脚本时,php 显示 %u2019 而不是引号等...
我是这样解决的:
- 在我的 javascript 脚本中,在发送之前将
escape(myvar)
替换为encodeURIComponent(myvar)
- 在我的 php 文件中,使用
$myvar=utf8_decode($_POST['myvar'])
希望能有所帮助。
I had this kind of problem.
When i send variable via javascript/ajax to a php script, the php was displaying %u2019 instead of quote, etc...
I solved it like this :
- in my javascript script, replacing
escape(myvar)
byencodeURIComponent(myvar)
before sending- in my php file, using
$myvar=utf8_decode($_POST['myvar'])
Hope thant can help.