Jump to content

Sanitizing Hashtags from # to %23 to create a tweetable tweet?


hollyvalero
 Share

Recommended Posts

I have a text field called tweet.  I'd like to display it:

$tweet:  This is a sample tweet to @hollyvalero using a #hashtag, or #twotags

and create a tweet button to convert that tweet as postable... I've removed the whitespace and replaced it with + signs

        if ($item->tweet) {
                $tweetable = $sanitizer->removeWhitespace($item->tweet, ['replace' => '+']);
                echo "<div class='twitterformatted'>{$tweetable}</div>";
             }

this gives me: This+is+a+sample+tweet+to+@hollyvalero+using+a+#hashtag,+or+#twotags

in a link tag, the @handle works fine, but I need to replace the #hashtag with %23hashtag

Trying to use a two-step approach with sanitize->chars but I don't see how to turn: $replacement = ''  into replace # with '%23'  ... 

         $hashtag = $sanitizer->chars($item->tweet, '%23' = '#');
         $tweetable = $sanitizer->removeWhitespace($hashtag, ['replace' => '+']);

How do I word this?

Link to comment
Share on other sites

By 'convert that tweet as postable' you mean format it to post it to the Twitter API? In this case, what you want to use urlencode instead of the sanitizer.

Quote

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

Depending on what you want to do rawurlencode might be a better option. It encodes spaces as %20 instead of plus signs, which is technically correct according to the relevant RFC.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...