Yesterday I have just released some of my graphics for Twitter that I have designed myself.
This time I will show you how can easily create links to your own pages on your website or different articles whenever you have both a page URL and a page title available.
This code automatically removes characters from the end of the title so that in addition to the URL itself and a space character between the maximum length of 140 characters will not be exceeded. If the title is too long it will be truncated and three dots will be appended to show that it has been shortened.
When clicking on the link the user is taken to the Twitter homepage with the message already preset so that he can tweet it immediately with just a single click.
function getTweetUrl($url, $title)
{
$maxTitleLength = 140 - (strlen($url)+1);
if (strlen($title) > $maxTitleLength) {
$title = substr($title, 0, ($maxTitleLength-3)).'...';
}
$output = "$title $url";
return 'http://twitter.com/home?status='.urlencode($output);
}
?>
Example usage:
<a href="<?php echo $url; ?>" target="_blank"><img src="tweetthis.gif" border="0" alt="Tweet This!" /></a>

































Great Page Brother.
I downloaded the buttons. Thank you for the gift of giving up front.
I will keep an eye on what you are doing and do a write up on you in the very near future!
Rhonda Giarraffa
Affiliate Review Guru
http://www.affiliatereviewguru.com
This won’t work. No where in your example do you actually call the getTweetUrl() function
So, what will happen is absolutely nothing when the link is clicked. Just an FYI.
Nevermind what I just said. I see what the script is supposed to do
I was reading the description as to what it was supposed to do incorrectly…
Another good piece of code, personally I prefer php over Java for such. I feel that more browsers find php acceptable.
This is a great script and so is your html script. The html script I can get working the php script I am confused on how to implement it. Where does the actual fuction go so it can be called?
Thank you
DeZiner
Great script. Thanks. And if you add onclick=”w=window.open(this.href,\’tweet\’,\’width=800,height=450,scrollbars,resizable,left=0,top=0\’); w.focus(); return false;\”
between the a and href, and remove the target=blank, you can get it to open in a popup window instead.
Best wishes
Tony
Is there a way to shorten the url?
Not currently.
Is there a way to add the ability to add “#Stuff” to it taking that into account with the character limit but being sure to keep it in? So people can see topics.
Do you have any idea to make a “share this” button for http://my-status.net ?
it’s a new social media based on microblogging platform like Twitter..
Thanks for a nice post.
I’ve just reported a way to automatically tweet at http://graham-kendall.com/blog/?p=103/, which seems to work pretty well (at least at the moment :-)).
I have also recently implemented automatic shortening of URLs (using the bit.ly API), which I’ll blog about soon, as well being able to tweet (on average) a set number of times a day (again, blog post coming soon).
You can shorten any url using a service like http://twig.mx just make the url call their api like this and make sure you use the http prefix in the url variable like this. If your url already has the http prefix, then just leave off the http prefix in the call to twig.
$newurl = “http://www.twig.mx/api-create.php?url=http://” . $url;
$shorturl = file_get_contents($newurl);
or
$newurl = “http://www.twig.mx/api-create.php?url=” . $url;
$shorturl = file_get_contents($newurl);
The $shorturl will contain a shortened url.
Then use the $shorturl in the HREF as shown above.