我正在编写一个带有自定义推文按钮的网站,该按钮使用 www.twitter.com/share 功能,但是我遇到的问题是在推文文本中包含哈希“#”字符。
例如:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+#branstonpickel+right+now
推文文本显示为“我正在吃东西”,并省略了哈希值和后面的所有内容。
我快速浏览了一下 Twitter 论坛,发现哈希“#”字符不能成为共享网址的一部分。
https://dev.twitter.com/discussions/512#comment-877 据说:
哈希值是 URL 中的特殊字符(它们标识文档片段),因此它们以及后面的任何内容都不会发送到服务器。
和
您需要对其进行 URLEncode,因此请使用 %23
当我尝试测试链接中的第二点时:
www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branstonpickel+现在+现在
推文文本显示为“我现在正在吃 %23branstonpickel”,字面上包括 %23,而不是将其转换为哈希值。
很抱歉问了这个愚蠢的问题,但有人知道我做错了什么吗?
任何反馈将不胜感激:)
I'm writing a site with a custom tweet button that uses the www.twitter.com/share function, however the problem I am having is including hash '#' characters within the tweet text.
For example:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+#branstonpickel+right+now
The tweet text comes out as 'I am eating' and omits the hash and everything after.
I had a quick look on the Twitter forums and learnt the hash '#' character cannot be part of the share url.
On https://dev.twitter.com/discussions/512#comment-877 it was said that:
Hashes are special characters in the URL (they identify document fragments) so they, and anything following, does not get sent the server.
and
you need to URLEncode it, so use %23
When I tried the 2nd point in my test link:
www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branstonpickel+right+now
The tweet text came out as 'I am eating %23branstonpickel right now' literally including %23 instead of converting it to a hash.
Sorry for the waffely question, but does anyone know what it is I'm doing wrong?
Any feedback would be greatly appreciated :)
发布评论
评论(9)
看起来这是基本设置:
这将预先构建一条推文: <网址>
上面的例子是:
曾经有一个关于主题标签参数的错误...它只显示前 n-1 个主题标签。目前此问题已修复。
It looks like this is the basic setup:
This would pre-built a tweet of:
<text> <url> <hashtags>
The above example would be:
https://twitter.com/intent/tweet?url=http://www.example.com&text=I+am+eating+branston+pickel+right+now&hashtags=bransonpickel,pickles
There used to be a bug with the hashtags parameter... it only showed the first n-1 hashtags. Currently this is fixed.
您可以在网址中使用 %23 代替哈希 (#),例如
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branston+%23pickel+right+now
you can use %23 instead of hash (#) in url eg
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branston+%23pickel+right+now
我可能是错的,但我认为主题标签必须作为单独的变量传递,该变量将出现在推文的末尾,即:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+branston+pickel+right+now&hashtag=bransonpickel
将结果是“我现在正在吃布兰斯顿泡菜#branstonpickle”
在另一个说明中,我认为泡菜应该是泡菜!
干杯
托比
I may be wrong but i think the hashtag has to be passed as a separate variable that will appear at the end of your tweet ie:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+branston+pickel+right+now&hashtag=bransonpickel
will result in "I am eating branston pickel right now #branstonpickle"
On a separate note, I think pickel should be pickle!
Cheers
Toby
使用encodeURIComponent对url进行编码
use encodeURIComponent to encode the url
如果您使用 PHP,则可以使用以下命令:
这将为您完成所有 URL 编码,并且很容易阅读。
有关 http_build_query 的更多信息,请参阅 PHP 手册:
https://www.php.net/http_build_query
If you're using PHP, you can use the following:
This will do all the URL encoding for you, and it's easy to read.
For more information on the http_build_query, see the PHP manual:
https://www.php.net/http_build_query
在 JS 中,您可以使用
encoreURIComponent 轻松对特殊字符进行编码
。(警告:不要使用
encodeURI
因为“#”和“@”不会被转义。)这是一个包含提及和主题标签的示例:
In JS you can easily encode the special characters using
encoreURIComponent
.(Warning: don't use
encodeURI
as "#" and "@" are not escaped.)Here's an example with mention and hashtag:
对于包含
line Jump
、#
、@
和特殊 unicode 的 url,以下方法有效:For url with
line jump
,#
,@
and special unicode in it, the following works :urlencode
https://twitter.com/intent/tweet?text=”
urlencode
https://twitter.com/intent/tweet?text=<?= urlencode("I am eating #branstonpickel right now"); ?>"
你可以直接使用这段代码并修改它
20% 意味着空间
23% 表示主题标签
You can just use this code and modify it
20% means space
23% means hashtag