Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 293823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:18:39+00:00 2026-05-12T06:18:39+00:00

Is there a better way to format text from Twitter to link the hyperlinks,

  • 0

Is there a better way to format text from Twitter to link the hyperlinks, username and hashtags? What I have is working but I know this could be done better. I am interested in alternative techniques. I am setting this up as a HTML Helper for ASP.NET MVC.

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace Acme.Mvc.Extensions
{

    public static class MvcExtensions
    {
        const string ScreenNamePattern = @"@([A-Za-z0-9\-_&;]+)";
        const string HashTagPattern = @"#([A-Za-z0-9\-_&;]+)";
        const string HyperLinkPattern = @"(http://\S+)\s?";

        public static string TweetText(this HtmlHelper helper, string text)
        {
            return FormatTweetText(text);
        }

        public static string FormatTweetText(string text)
        {
            string result = text;

            if (result.Contains("http://"))
            {
                var links = new List<string>();
                foreach (Match match in Regex.Matches(result, HyperLinkPattern))
                {
                    var url = match.Groups[1].Value;
                    if (!links.Contains(url))
                    {
                        links.Add(url);
                        result = result.Replace(url, String.Format("<a href=\"{0}\">{0}</a>", url));
                    }
                }
            }

            if (result.Contains("@"))
            {
                var names = new List<string>();
                foreach (Match match in Regex.Matches(result, ScreenNamePattern))
                {
                    var screenName = match.Groups[1].Value;
                    if (!names.Contains(screenName))
                    {
                        names.Add(screenName);
                        result = result.Replace("@" + screenName,
                           String.Format("<a href=\"http://twitter.com/{0}\">@{0}</a>", screenName));
                    }
                }
            }

            if (result.Contains("#"))
            {
                var names = new List<string>();
                foreach (Match match in Regex.Matches(result, HashTagPattern))
                {
                    var hashTag = match.Groups[1].Value;
                    if (!names.Contains(hashTag))
                    {
                        names.Add(hashTag);
                        result = result.Replace("#" + hashTag,
                           String.Format("<a href=\"http://twitter.com/search?q={0}\">#{1}</a>",
                           HttpUtility.UrlEncode("#" + hashTag), hashTag));
                    }
                }
            }

            return result;
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:18:39+00:00Added an answer on May 12, 2026 at 6:18 am

    That is remarkably similar to the code I wrote that displays my Twitter status on my blog. The only further things I do that I do are

    1) looking up @name and replacing it with <a href="http://twitter.com/name">Real Name</a>;

    2) multiple @name‘s in a row get commas, if they don’t have them;

    3) Tweets that start with @name(s) are formatted “To @name:”.

    I don’t see any reason this can’t be an effective way to parse a tweet – they are a very consistent format (good for regex) and in most situations the speed (milliseconds) is more than acceptable.

    Edit:

    Here is the code for my Tweet parser. It’s a bit too long to put in a Stack Overflow answer. It takes a tweet like:

    @user1 @user2 check out this cool link I got from @user3: http://url.com/page.htm#anchor #coollinks

    And turns it into:

    <span class="salutation">
        To <a href="http://twitter.com/user1">Real Name</a>,
        <a href="http://twitter.com/user2">Real Name</a>:
    </span> check out this cool link I got from
    <span class="salutation">
        <a href="http://www.twitter.com/user3">Real Name</a>
    </span>:
    <a href="http://site.com/page.htm#anchor">http://site.com/...</a>
    <a href="http://twitter.com/#search?q=%23coollinks">#coollinks</a>
    

    It also wraps all that markup in a little JavaScript:

    document.getElementById('twitter').innerHTML = '{markup}';
    

    This is so the tweet fetcher can run asynchronously as a JS and if Twitter is down or slow it won’t affect my site’s page load time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a better way to forcefully disconnect all users from an Oracle 10g
Is there a better way to flash a window in Java than this: public
Is there a better way of binding a list of base class to a
Is there a better way to do this? -(NSDate *)getMidnightTommorow { NSCalendarDate *now =
Is there a better way to debug JavaScript than MS Script Editor? I am
Is there a better way than simply trying to open the file? int exists(const
Is there a better way to negate a boolean in Java than a simple
Is there a better way to watch for new entries in a table besides
Is there a better way to do the following: $array = array('test1', 'test2', 'test3',
Is there a better way than the following to check to see if a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.