Hi everyone, I have created a twitter app to auto post to a number of twitter accounts.
The auto tweet works great - but for one thing I am stuck on...
I am trying to add a hashtag # (#) to the tweet but for some reason I'ts not working.

Do I need to use urlencode? im not how that would work.

Here is the working auto tweet - What I am trying to do is add the hashtag before $wastteam
So the tweet would look like this #Everton

<?php twitter_post($wastuname . ' ' . has . ' ' . just . ' ' . entered . ' ' . a . ' ' . competition . ' ' . to . ' ' . win . ' ' . a . ' ' . $wastteam . ' ' . season . ' ' . ticket . ' ' . at . ' ' .$link) ?>

If anyone could help that would be great thanks -

Dani AI

Generated

As discovered, the fix is to include the literal # when you build the tweet text — do not use HTML entities like &#35; or &amp;#35;. Those are for rendering HTML, not for the Twitter API or for a tweet-intent URL, and they end up being sent (or interpreted by the browser) as the wrong thing.

HTML escaping vs URL/API encoding:

  • htmlentities() is only for safely embedding text inside HTML. It converts # into entity text, which you do not want when posting to Twitter.
  • If you are building a query string or a tweet-intent link (https://twitter.com/intent/tweet?text=...), percent-encode the whole text (so # becomes %23) using rawurlencode() or urlencode().
  • If you are posting to the Twitter API (POST statuses/update), construct a plain UTF-8 string with the # included; let your OAuth library handle encoding, or use http_build_query()/rawurlencode() correctly if you build the request yourself.

Practical examples (safe patterns):

$status = sprintf('%s entered to win a #%s season ticket: %s', $wastuname, $wastteam, $link);

// For intent link:
$intent = 'https://twitter.com/intent/tweet?text=' . rawurlencode($status);

// For API POST: send $status as the status parameter (let your OAuth library encode it)

Quick checks and cautions:

  • Hashtags cannot contain spaces or punctuation; sanitize $wastteam if it may include them (replace spaces or strip unwanted chars).
  • Only escape for HTML when outputting the tweet into a web page (to prevent XSS); do not HTML-escape the text you send to Twitter.
  • ’s suggestion to simplify string construction avoids accidental double-encoding and makes it easier to insert the # cleanly.

Recommended Answers

All 4 Replies

Member Avatar for Member #120589

Impossible to say unless we can see the code. ALso you don't need all that concatenation:

<?php twitter_post("$wastuname has just entered a competition to win a $wastteam season ticket at $link");?>

will work

Hi thanks -

I have tried using
$hashtag=htmlentities("'.&#35;.'");
$hashtags=htmlentities("'.#.'");
$hashtagss=htmlentities("&amp;#35;");

But still not getting the # symbol

Member Avatar for Member #120589

We have no way of knowing how you're passing this info or how it's being used. Show your code.

Thanks for your reply, looks like I going about it the wrong way :)

I added the # before $wastteam and bingo -
Thanks buddy -

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.