如何在 Twitter 共享链接文本中包含主题标签?

发布于 2025-01-02 03:04:26 字数 1016 浏览 1 评论 0 原文

我正在编写一个带有自定义推文按钮的网站,该按钮使用 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 :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

那片花海 2025-01-09 03:04:26

看起来这是基本设置:

https://twitter.com/intent/tweet?
url=<url to tweet>
text=<text to tweet>
hashtags=<comma separated list of hashtags, with no # on them>

这将预先构建一条推文: <网址>

上面的例子是:

曾经有一个关于主题标签参数的错误...它只显示前 n-1 个主题标签。目前此问题已修复。

It looks like this is the basic setup:

https://twitter.com/intent/tweet?
url=<url to tweet>
text=<text to tweet>
hashtags=<comma separated list of hashtags, with no # on them>

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.

落墨 2025-01-09 03:04:26

我可能是错的,但我认为主题标签必须作为单独的变量传递,该变量将出现在推文的末尾,即:

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

|煩躁 2025-01-09 03:04:26

使用encodeURIComponent对url进行编码

use encodeURIComponent to encode the url

雨轻弹 2025-01-09 03:04:26

如果您使用 PHP,则可以使用以下命令:

<?php echo 'http://www.twitter.com/share?' . http_build_query(array(
    'url' => 'http://www.example.com',
    'text' => 'I am eating #branstonpickel right now'
)); ?>

这将为您完成所有 URL 编码,并且很容易阅读。

有关 http_build_query 的更多信息,请参阅 PHP 手册:
https://www.php.net/http_build_query

If you're using PHP, you can use the following:

<?php echo 'http://www.twitter.com/share?' . http_build_query(array(
    'url' => 'http://www.example.com',
    'text' => 'I am eating #branstonpickel right now'
)); ?>

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

孤独陪着我 2025-01-09 03:04:26

在 JS 中,您可以使用 encoreURIComponent 轻松对特殊字符进行编码

(警告:不要使用 encodeURI 因为“#”和“@”不会被转义。)

这是一个包含提及和主题标签的示例:

const text = "Hello #world ! Go follow @StackOverflow";
const tweetUrl = `https://twitter.com/intent/tweet?text=${ encodeURIComponent(text) }`;

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:

const text = "Hello #world ! Go follow @StackOverflow";
const tweetUrl = `https://twitter.com/intent/tweet?text=${ encodeURIComponent(text) }`;
一抹淡然 2025-01-09 03:04:26

对于包含 line Jump#@ 和特殊 unicode 的 url,以下方法有效:

var lineJump = encodeURI(String.fromCharCode(10)),
hash = "%23", arobase="%40",
tweetText = 'https://twitter.com/intent/tweet?text=Le signe chinois '+hans+' '+item.pinyin+': '+item.definition.replace(";",",")+'.'
+lineJump+'Merci '+arobase+'Inalco_Officiel '+arobase+'CRIparis ❤️

For url with line jump, # , @ and special unicode in it, the following works :

var lineJump = encodeURI(String.fromCharCode(10)),
hash = "%23", arobase="%40",
tweetText = 'https://twitter.com/intent/tweet?text=Le signe chinois '+hans+' '+item.pinyin+': '+item.definition.replace(";",",")+'.'
    +lineJump+'Merci '+arobase+'Inalco_Officiel '+arobase+'CRIparis ❤️???????? '
    +lineJump+hash+'Chinois '+hash+'MOOC'
    +lineJump+'https://hanzi.cri-paris.org/',
tweetTxtUrlEncoded = tweetText+ "" +encodeURIComponent('#'+lesson+encodeURIComponent(hans));
じ违心 2025-01-09 03:04:26

urlencode

https://twitter.com/intent/tweet?text=

urlencode

https://twitter.com/intent/tweet?text=<?= urlencode("I am eating #branstonpickel right now"); ?>"

心凉怎暖 2025-01-09 03:04:26

你可以直接使用这段代码并修改它

<a href="https://twitter.com/intent/tweet?text=hello%20this%20%23hashtag"> </a>

20% 意味着空间
23% 表示主题标签

You can just use this code and modify it

<a href="https://twitter.com/intent/tweet?text=hello%20this%20%23hashtag"> </a>

20% means space
23% means hashtag

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