Twitter-Plugin Linkify Code-Snippet

1 view
Skip to first unread message

Florian Fiegel

unread,
Feb 12, 2009, 9:12:27 AM2/12/09
to habari-dev
Hi out there,

I am not so firm to the things of the repo/svn/trac, just learning to
work with it. When working on a page we made up the Linkify of the
Twitter-Plugin and fixed it.

Here is the Snippet that someone should work into the code:

// replace urls with links
$tweet->text = preg_replace("/(http:\/\/\S*)/", "<a href=\"$1\">$1</
a>", $tweet->text);
// link twitter usernames
$tweet->text = preg_replace("/(@(\S*))/", "<a href=\"http://
twitter.com/$2\">$1</a>", $tweet->text);
// link to hash tags (maaaan are web 2.0 or w00t?!)
$tweet->text = preg_replace("/(#(\S*))/", "<a href=\"http://
hashtags.org/search?query=$2&submit=Search\">$1</a>", $tweet->text);

regards
Florian

mikelietz

unread,
Feb 17, 2009, 12:56:50 PM2/17/09
to habari-dev
Thanks for this, Florian, but I'm having one problem with it - it
doesn't seem to get rid of punctuation. So if the update is:

@florian What links to #habari? What about http://habariproject.org?

the links have the question marks in them.
--mikelietz

On Feb 12, 9:12 am, Florian Fiegel <florian.fie...@googlemail.com>
wrote:

Geoffrey Sneddon

unread,
Feb 17, 2009, 1:58:46 PM2/17/09
to habar...@googlegroups.com

On 17 Feb 2009, at 17:56, mikelietz wrote:

> Thanks for this, Florian, but I'm having one problem with it - it
> doesn't seem to get rid of punctuation. So if the update is:
>
> @florian What links to #habari? What about http://habariproject.org?
>
> the links have the question marks in them.

Having quite comprehensively having spammed Twitter, their
linkification does the following:

+-----------------------------------------------------------------+
| URI | Linked To |
+--------------------------------+--------------------------------+
| http://example.com/ | http://example.com/ |
| http://example.com/foobar/ | http://example.com/foobar/ |
| http://www.詹姆斯.com/ | http://www.詹姆
斯.com/ |
| http://[::1]/ | |
| http://example.com/foo?bar#lol | http://example.com/foo?bar#lol |
| ftp://example.com | |
| http://example.com1 | http://example.com1 |
| http://example.com. | http://example.com |
| http://example.com? | http://example.com |
| http://example.com/foobar? | http://example.com/foobar |
| http://example.com# | http://example.com# |
| http://example.com: | http://example.com |
| http://example.com:80 | http://example.com:80 |
| http://foo;bar.com/ | http://foo |
| http://ex&mple.com | http://ex |
| http://example.com" | http://example.com |
| http://example.com! | http://example.com |
| http://example.com$ | http://example.com |
| http://example.com& | http://example.com |
| http://example.com' | http://example.com |
| http://example.com( | http://example.com |
| http://example.com) | http://example.com |
| http://example.com* | http://example.com |
| http://example.com+ | http://example.com |
| http://example.com, | http://example.com |
| http://example.com= | http://example.com |
| http://ex%60mple.com | http://ex |
| HtTp://eXaMpLe.CoM | |
| http://EXAMPLE.COM | http://example.com |
| HTTP://EXAMPLE.COM | |
+--------------------------------+--------------------------------+

--
Geoffrey Sneddon
<http://gsnedders.com/>

Fiegel Florian

unread,
Feb 18, 2009, 8:38:32 AM2/18/09
to habar...@googlegroups.com
Maybe their will be further fixes in near future I will give it back
to the one who made it up.

We also work on something else (coming soon) and fixed it just cause
we build upon original twitter plugin.

So maybe their will be a better version in near future :)

regards
Florian

> --~--~---------~--~----~------------~-------~--~----~
> This message is part of the topic "Twitter-Plugin Linkify Code-
> Snippet"
> in the Google Group "habari-dev" for which you requested email
> updates.
> To stop receiving email updates for this topic, please visit the topic
> at http://groups.google.com/group/habari-dev/t/691194d7526ebb29
> -~----------~----~----~----~------~----~------~--~---
>

mikelietz

unread,
Feb 18, 2009, 10:29:52 AM2/18/09
to habari-dev
Speaking of further fixes, here is the regex I ended up committing:

/* link to all http: */
$theme->tweet_text = preg_replace( '%https?://\S+?(?=(?:[.:?"!$&\'()*
+,=]|)(?:\s|$))%i', "<a href=\"$0\">$0</a>", $theme->tweet_text );
/* link to usernames */
$theme->tweet_text = preg_replace( "/(?<!\w)@([\w-_.]{1,64})/", "@<a
href=\"http://twitter.com/$1\">$1</a>", $theme->tweet_text );
/* link to hashtags */
$theme->tweet_text = preg_replace( '/(?<!\w)#((?>\d{1,64}|)[\w-.]
{1,64})/',
"<a href=\"" . Options::get('twitter__hashtags_query') ."$1\">#$1</
a>", $theme->tweet_text );

Seems to work and match very closely to the behavior of the twitter
linkification.
--mikelietz

On Feb 18, 8:38 am, Fiegel Florian <florian.fie...@googlemail.com>
wrote:
> Maybe their will be further fixes in near future I will give it back  
> to the one who made it up.
>
> We also work on something else (coming soon) and fixed it just cause  
> we build upon original twitter plugin.
>
> So maybe their will be a better version in near future :)
>
> regards
> Florian
>
> Am 17.02.2009 um 19:58 schrieb Geoffrey Sneddon:
>
>
>
> > On 17 Feb 2009, at 17:56, mikelietz wrote:
>
> >> Thanks for this, Florian, but I'm having one problem with it - it
> >> doesn't seem to get rid of punctuation. So if the update is:
>
> >> @florian What links to #habari? What abouthttp://habariproject.org?
>  smime.p7s
> 3KViewDownload

Caius Durling

unread,
Feb 18, 2009, 10:34:57 AM2/18/09
to habar...@googlegroups.com
On 18 Feb 2009, at 15:29, mikelietz wrote:

Seems to work and match very closely to the behavior of the twitter
linkification.

I mentioned this on IRC, but for future reference twitter uses the rails method auto_link from the looks of it.


PGP.sig
Reply all
Reply to author
Forward
0 new messages