0

I have the following method for replacing emoticons in a string using C#

    public static string Emotify(string inputText)
    {
        var emoticonFolder = EmoticonFolder;
        var emoticons = new Hashtable(100)
        {
            {":)", "facebook-smiley-face-for-comments.png"},
            {":D", "big-smile-emoticon-for-facebook.png"},
            {":(", "facebook-frown-emoticon.png"},
            {":'(", "facebook-cry-emoticon-crying-symbol.png"},
            {":P", "facebook-tongue-out-emoticon.png"},
            {"O:)", "angel-emoticon.png"},
            {"3:)", "devil-emoticon.png"},
            {":/", "unsure-emoticon.png"},
            {">:O", "angry-emoticon.png"},
            {":O", "surprised-emoticon.png"},
            {"-_-", "squinting-emoticon.png"},
            {":*", "kiss-emoticon.png"},
            {"^_^", "kiki-emoticon.png"},
            {">:(", "grumpy-emoticon.png"},
            {":v", "pacman-emoticon.png"},
            {":3", "curly-lips-emoticon.png"},
            {"o.O", "confused-emoticon-wtf-symbol-for-facebook.png"},
            {";)", "wink-emoticon.png"},
            {"8-)", "glasses-emoticon.png"},
            {"8| B|", "sunglasses-emoticon.png"}
        };

        var sb = new StringBuilder(inputText.Length);

        for (var i = 0; i < inputText.Length; i++)
        {
            var strEmote = string.Empty;
            foreach (string emote in emoticons.Keys)
            {
                if (inputText.Length - i >= emote.Length && emote.Equals(inputText.Substring(i, emote.Length), StringComparison.InvariantCultureIgnoreCase))
                {
                    strEmote = emote;
                    break;
                }
            }

            if (strEmote.Length != 0)
            {
                sb.AppendFormat("<img src=\"{0}{1}\" alt=\"\" class=\"emoticon\" />", emoticonFolder, emoticons[strEmote]);
                i += strEmote.Length - 1;
            }
            else
            {
                sb.Append(inputText[i]);
            }
        }
        return sb.ToString();
    }

It works great and 'seems' pretty fast, however I realised a slight problem with Html.

This method breaks pages with a link in them because of the..

:/

emoticon. It breaks the

http:// 

By sticking an image in the middle. I'm trying to figure out a way to adapt this method to take into account links and ignore them - But without sacrificing performance.

Any help or pointers greatly appreciated.

YodasMyDad
  • 9,248
  • 24
  • 76
  • 121
  • Have you considered using regex matching so as to only look in strings or to ignore strings with http(s):/? – JustMeToo Sep 02 '15 at 19:32
  • Another thing to consider if talking about HTML: is ">:(" grumpy, or is that a close tag followed by a frown? Just asking so you think about it. – Curtis Sep 02 '15 at 19:38
  • Instead of `HashTable` use the generic version `Dictionary`. – juharr Sep 02 '15 at 19:38
  • http://stackoverflow.com/questions/15944495/prevent-replacements-of-emoticons-in-html-tags?rq=1 (PHP, but the same approaches that will be suggested for C# anyway, so get head start). Also consider clarifying for yourself when you want to detect emoticon - possibly whitespace around? Normally HTMLAgilityPack would be useful start, but it is not going to help much as text can have HTMLs and all kind of encoded `>)` text. – Alexei Levenkov Sep 02 '15 at 19:42
  • You could make a `List` of `keywords` that it checks before transformation to an emoticon. It would take the found emoticon, and check surrounding characters to see if it matches a keyword. If so, ignore it and continue. – TestWell Sep 02 '15 at 19:43
  • You could modify your algorithm so that it searches only for whole words, ie. those surrounded by spaces. – Kevin Sep 02 '15 at 20:34
  • HTML agility pack and regex will be your friend here. You could have a decorator where your decorations build up the src? – Charlie Afford Sep 03 '15 at 06:16

1 Answers1

-1

HTML agility pack and regex will be your friend here. You could have a decorator where your decorations build up the src?. Can we have an example of the src that causes the issue? :)

  • Just a normal link, ie. anything that starts with http:// or the end of a html tag. For example, the end of a p tag.. p> with a :( emoticon in, will get caught as >:( which is a different emoticon. It's a bit of a minefield... – YodasMyDad Sep 04 '15 at 08:01
  • I don't envy you, I will have a think, let us know if you find a solution :) – Charlie Afford Sep 11 '15 at 21:38