How do you include hashtags within Twitter share link text?

末鹿安然 提交于 2019-12-03 04:10:12

问题


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


回答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.




回答2:


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




回答3:


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




回答4:


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: http://us2.php.net/http_build_query




回答5:


use encodeURIComponent to encode the url




回答6:


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


来源:https://stackoverflow.com/questions/9127808/how-do-you-include-hashtags-within-twitter-share-link-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!