Twitter Share code - %20?

Hi guys,

I hope this is the appropriate Forum for this. I am doing a “share to twitter” button on one of my websites.

On my blog, it works great, I have the following:

<a href="LINKTOTWITTER/home?status=<?php the_title() ?> <?php the_permalink() ?>" title="Submit to Twitter" target="_blank">Twitter</a>

However, on my Video Sharing site, it doesn’t work properly (this time its using an image):

<a href="LINKTOTWITTER/home?status={$view.video_info.video_title} {$base_url}/view/{$view.video_info.video_id}/{$view.video_info.video_seo_name}/" title="Twitter"><img src="{$img_css_url}/images/twitter.jpg" border="0" alt="twitter" /></a>

When hovering over the image, the format looks exactly the same as the one on my blog, yet clicking it causes the status to have “%20” every time there is a space.

Any help appreciated, thanks.

My question is, WHY are the spaces in the first place?

Instead of converting spaces, they should be removed: en.wikipeda.org/wiki/Some Thing (or en.wikipedia.org/wiki/Some+Thing) and
en.wikipedia.org/wiki/SomeThing

should be two different urls to two different things.

That means spaces count. So whether they are changed to %20 or not shouldn’t matter: you’ve got to get them out beforehand.

If you can’t write them with the spaces (or are they really newlines?) out, can you regex them out?

You could use warfman’s code but add in there ’ ’ and set that to ‘’ (unless you do have URLs who really do have spaces in them, but in general I would avoid them if you can).

URL is gone so I can’t see what the output is right now.

Hi guys,

I implemented something for my wordpress blog, but I think it might be helpful for any site.

I placed this code at the top of the page that I needed to encode a string:


<?php
$find = array('!', '*', ':', '&', '=', '+', '$', '#');
$replace = array('%21', '%2A', '%3A', '%26', '%3D', '%2B', '%24', '%23');
?>

And then place this code where you need to encoding to happen:


<?php echo str_replace($find, $replace, $post->post_title); ?>

The “$post->post_title” is specific to wordpress. It pulls in the title of the post. So just replace that with whatever you need encoded.

Also, you can add additional encodings to the $find and $replace variables at the top of the page. I chose to use only certain ones that twitter needs.

Yeah, perhaps it’s better to ask this in the PHP forum.

Anyhow, wordPress is clever enough to remove the gaps between words–and replace them with a + instead, which stops the %20 appearing.

You could try adding things like this to the top of your page (assuming it is a .php page):

<?php
	$view.video_info.video_title = "My great video";
	$view.video_info.video_title1 = str_replace(" ","+",$view.video_info.video_title);
	$view.video_info.video_id = "My video id";
	$view.video_info.video_id1 = str_replace(" ","+",$view.video_info.video_id);
	$view.video_info.video_seo_name = "My great seo name";
	$view.video_info.video_seo_name1 = str_replace(" ","+",$view.video_info.video_seo_name);
?>

… and then change the link to (changes in red):

<a href="LINKTOTWITTER/home?status={$view.video_info.video_title[COLOR="Red"]1[/COLOR]}[COLOR="Red"]+[/COLOR]{$base_url}/view/{$view.video_info.video_id[COLOR="Red"]1[/COLOR]}/{$view.video_info.video_seo_name[COLOR="Red"]1[/COLOR]}/" title="Twitter"><img src="{$img_css_url}/images/twitter.jpg" border="0" alt="twitter" /></a>

Not very sophisticated, but it replaces each variable with a version that has plusses between each word rather than a gap.

Go to the PHP forum if this doesn’t work. :slight_smile:

Actually, on second thoughts, the PHP I posted above should probably be simplified to just this:

<?php
    $view.video_info.video_title1 = str_replace(" ","+",$view.video_info.video_title);
    $view.video_info.video_id1 = str_replace(" ","+",$view.video_info.video_id);
    $view.video_info.video_seo_name1 = str_replace(" ","+",$view.video_info.video_seo_name);
?>

Remember to place this code right at the top of your page, without a single space before the initial <?php opening.

Hey, thanks for your help.

The file that seems to control the design is a “.tpl” file, so I assume I will not be able to rename that as it holds the template. It seems to be full of HTML.

Is there any way I can “call” that script using HTML?

thanks

A .tpl file is a template file, and as far as I know (from a quick Google) it is recognized by PHP. So, if you are lucky, it will just work if you paste that PHP code into your file.

If it doesn’t work, maybe head over to the PHP forum and ask them what to do. I’m just a PHP noob.

Thanks, I copied the code exactly, and clicked the twitter button and it still had those %20s in. I’ll have a look in the PHP forum shortly.

Did you also modify the link to this?

<a href="LINKTOTWITTER/home?status={$view.video_info.video_title1}+{$base_url}/view/{$view.video_info.video_id1}/{$view.video_info.video_seo_name1}/" title="Twitter"><img src="{$img_css_url}/images/twitter.jpg" border="0" alt="twitter" /></a>

Oops, forgot to do that. Hmm, it shows up differently, now I can show you the example. Little twitter button on the right:

http://mega-byte.tv/view/11/how-to-make-firefox-start-in-private-browsing-mode/

Hmm, so those new variables are just not showing up. Meh, it was worth a try.

I’m sure vShare has as answer for this, but it’s above my head. sorry!

Actually, the Digg link works, so I am wondering what code is used for that link? Usually Digg has the same problem with the %20 thing. Could you post the Digg link code? It might give the clue to the Twitter code.