Tech Note: A Regular Expression to Convert Hashtags and Twitter Names to Links

Expressions that are regular
LGF • Views: 12,293
Image via Shutterstock

Once in a while I come up with a bit of code I think might be useful to others, and here’s the latest example: a regular expression that parses a string of text looking for Twitter hashtags (preceded with ‘#’) or usernames (preceded with ‘@’), and replaces them with HTML links to those pages at twitter.com. Every bit of text posted at LGF in a comment, page or article gets passed through this code.

It’s written in PHP, and uses preg_replace_callback() to process the matches with an anonymous function.

The regular expression is fairly sophisticated; named capturing groups are used to make it easier to reference the groups (instead of counting up parentheses), and it uses positive lookbehind combined with a lookaround conditional in order to avoid converting patterns that look like hashtags or usernames if they’re already contained within an HTML link.

Don’t panic, I won’t go into any more depth on the regular expression; I’ll leave that as an exercise for those who may be so inclined.

$text = preg_replace_callback(
	'~
		(?<=
			^
			|
			(?<=
				[^a-zA-Z0-9-.&]
			)
		)
		(?(?=
				]*>.+?
			)
			(?:
				]*>.+?
			)
			|
			(?
				[@#]{1}
			)
			(?
				[A-Za-z_]+[A-Za-z0-9_]+
			)
		)
	~xsi',
	function($matches) {
		if ($matches['name']) {
			return (
				'#' 
					: $matches['name'] . '/with_replies">@'
				) . $matches['name'] . ''
			);
		} else {
			return $matches[0];
		}
	},
	$text
);

Jump to top

Create a PageThis is the LGF Pages posting bookmarklet. To use it, drag this button to your browser's bookmark bar, and title it 'LGF Pages' (or whatever you like). Then browse to a site you want to post, select some text on the page to use for a quote, click the bookmarklet, and the Pages posting window will appear with the title, text, and any embedded video or audio files already filled in, ready to go.
Or... you can just click this button to open the Pages posting window right away.
Last updated: 2023-04-04 11:11 am PDT
LGF User's Guide RSS Feeds

Help support Little Green Footballs!

Subscribe now for ad-free access!Register and sign in to a free LGF account before subscribing, and your ad-free access will be automatically enabled.

Donate with
PayPal
Cash.app
Recent PagesClick to refresh
Once Praised, the Settlement to Help Sickened BP Oil Spill Workers Leaves Most With Nearly Nothing When a deadly explosion destroyed BP’s Deepwater Horizon drilling rig in the Gulf of Mexico, 134 million gallons of crude erupted into the sea over the next three months — and tens of thousands of ordinary people were hired ...
Cheechako
1 hour ago
Views: 33 • Comments: 0 • Rating: 0
Texas County at Center of Border Fight Is Overwhelmed by Migrant Deaths EAGLE PASS, Tex. - The undertaker lighted a cigarette and held it between his latex-gloved fingers as he stood over the bloated body bag lying in the bed of his battered pickup truck. The woman had been fished out ...
Cheechako
3 days ago
Views: 154 • Comments: 0 • Rating: 1