Joss Posted January 22, 2015 Posted January 22, 2015 if someone can check I have done this right, I would be obliged as I ain't no module developer! This is a very simple text formatter that looks for @ and # and adds the appropriate links to twitter. So, @profile will become http://www.twitter.com/profile and #searchterm will become http://twitter.com/search?q=searchterm It uses preg_replace as a quick and dirty solution rather than the Twitter API - so no apps need to be created and so on. https://github.com/jsanglier/TextformaterTwitterReplace Obviously, if you use @something and #something regularly and DONT want them linked, then this is useless for you. But quite nice for blogging purposes Unsolicited thanks to Netcarver because I nicked one of his modules to work out how to do it Joss 9
Joss Posted January 22, 2015 Author Posted January 22, 2015 Thanks - but don't hold your breath for a run of them Loads of ideas, no skill! 1
Mike Rockett Posted January 22, 2015 Posted January 22, 2015 Great stuff, Joss I think that your expressions could be a little more specific to the type being checked for. For example, usernames are only allowed numbers, letters, and underscores. So that expression can be changed to: /[a-z0-9_]/i (note that I've removed the caps and added the case-insensitive flag on the end there). As for hashtags, those are a lot more complex. I don't think I'd be able to explain what on earth this is (I'm no regex junkie). Guess /(#\w+)/ would also suffice.
Joss Posted January 22, 2015 Author Posted January 22, 2015 Yeah, I copied and pasted the regex - it has always been greek to me. Of course, I can't make it completely fool proof - for instance a username that does not exist! "Did you mean....?" 1
Mike Rockett Posted January 22, 2015 Posted January 22, 2015 Hehe, well, it's a step in the right direction. ;-)
suntrop Posted January 22, 2015 Posted January 22, 2015 Nothing special about your module, but I think twitter only uses https. You can avoid a redirect, if you replace the http://. And – maybe its just me – you could add a config where users can choose whether links will open in a new window or not. Ahh … one more: adding a css class will make it possible to address those links. However, thanks for that module. I'll use it in an upcoming website
Joss Posted January 22, 2015 Author Posted January 22, 2015 Changed to https (serves me right for copying and pasting!) There are probably a few config options that could be added. Adding a class is a nice one if someone wants to open in a popup of some sort and wants to target it with jquery, for instance. I might have another poke at it next week
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now