The Sandbox Thread

3 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

HTML tags Striped - Plain text replace - HTML tags re-added

Back to Forum: The Sandbox
  • User profile image
    Manip

    In-line Comments don't work in this so I am using /* */ instead which don't show up in greean.

    String[] badRegX = { @"\bn[e3i1\W_]+g([\W_]*g)?([\W_]*l)?[\W_]*[ei\d][\W_]*[rt]s?\b" };

    MessageBox.Show(replaceBadStuff("hello <tag>nig<?>ger w<tag>orld", "[BAD!]", badRegX));


    wrote:
    private String replaceBadStuff(String data, String replacementText, String[] badWords)
            {
                int lastLessThan = 0;
                ArrayList tags = new ArrayList();
               
                // Moves recursively though the string looking for and removing tags
                // On finding a tag, store its information; restart search (in case of embedded tags!)
                for (int x = 0; x < data.Length; x++)
                {
                    if (data[x] == '<') { lastLessThan = x; }
                    else if (lastLessThan != 0 && data[x] == '>')
                    {
                        string temp = data.Substring(lastLessThan + 1, x - lastLessThan - 1);
                       
                        // We don't want bi<new line>t<new line>ch to be detected as a bad word
                        if (temp != "br" || temp != "/p")
                        {
                            tags.Add(temp);
                            tags.Add(lastLessThan);
                            data = data.Substring(0, lastLessThan) + data.Substring(x + 1);
                            lastLessThan = 0;
                            x = 0;
                        }
                    }
                }

                // For each "bad word" replace it with - replacementText
                for (int y = 0; y < badWords.Length; y++)
                {
                    data = new Regex(badWords[y]).Replace(data, replacementText);
                }

                // Re-Adds the tags from the top down (again for embedded tags)
                for (int q = tags.Count-2; q >= 0 ; q -= 2)
                {
                        data = data.Substring(0, (int)tags[q+1]) + "<" + tags[q].ToString() + ">" + data.Substring((int)tags[q+1], data.Length - ((int)tags[q+1]));
                }
                return data;

            }


  • User profile image
    Charles

    This particular implementation may not be used in it's current form, but we'll use some form of your algorithm. Or, we may just use this exact code. Smiley

    Thanks for the code!

    C

  • User profile image
    Manip

    Charles wrote:
    This particular implementation may not be used in it's current form, but we'll use some form of your algorithm. Or, we may just use this exact code. Smiley

    Thanks for the code!

    C


    Yeah that's fair enough. You might be able to make it faster or
    it might not fit any requirements you might have. I just hope
    you fix the original issue in one form or another. Smiley

    Edit: I just updated it because of a single digit error (2 instead of 1), prize to who finds it Smiley

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.