PHP URL 编码/解码表单字段中的漂亮引号%u2019

发布于 2024-09-12 09:21:32 字数 614 浏览 1 评论 0原文

由于某种原因,在从文本表单向 php 提交类似 Jack's Spindle 的字符串后,我得到:

Jack%u2019s Spindle

This is not PHP 的 urlencode() 会做什么,即 Jack%92s+Spindlerawurlencode() = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲欢浪云 2024-09-19 09:21:32

好吧,只有你能告诉我们它来自哪里。您从哪里获取文本以及将其提交给哪些转换?我承认我还没有看到那种编码策略。

也就是说,它与 Javascript 编码 UTF-16 代码单元的方式非常相似:\uXXXX,其中每个 X 代表一个十六进制字符。要将其转换为 HTML 实体,您可以执行以下操作:

preg_replace('/%u([a-fA-F0-9]{4})/', '&#x\\1;', $string)

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 each X represents a hexadecimal character. To convert it to HTML entities, you could do:

preg_replace('/%u([a-fA-F0-9]{4})/', '&#x\\1;', $string)
污味仙女 2024-09-19 09:21:32

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).

美羊羊 2024-09-19 09:21:32

我有这样的问题。
当我通过 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) by encodeURIComponent(myvar) before sending
- in my php file, using $myvar=utf8_decode($_POST['myvar'])

Hope thant can help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文