locked
Take out text RRS feed

  • Question

  • User-1569779262 posted

    Hi Everyone,

        I am using WYSIWYG Editor to input values like Product Short Description. It adds tag like <div>, <span>,<p>, etc. of it's own. Though in front end it displays correct info. but when I use substring to display say only first 50 characters of Short Description, then, it takes tags and display is not proper. What I require is that it should display only Text and not tag. How can I do that??? I had written query like:

    select

    SUBSTRING(Product_Desc,0,50) PrdDesc from db_CrystalTec.dbo.Product_tb

     

     

     

    Thursday, November 18, 2010 6:57 AM

All replies

  • User-964259575 posted

    One possibility could be to get the whole text from database, and then strip the tags, and after that take the first 50 characters. Maybe something like this.

    string productDescription = "whole text..."; // select Product_Desc from db_CrystalTec.dbo.Product_tb
    productDescription = StripTags(productDescription).Substring(0, 50);


    Where StripTags() is implemented for example like this.

    using System.Text.RegularExpressions;
    
    public static string StripTags(string html)
    {
        return Regex.Replace(html, "<[^>]*>", string.Empty);
    }


    Thursday, November 18, 2010 7:59 AM
  • User-1569779262 posted

    Hi anders,

       Thanks for Replying. The way you  purposed is really good. But still it leaves some tags like /n,etc. So, to overcome that I had created one procedure & then called it like :

    public static String GetStrippedString(String data, Int32 MaxLength)
        {
            data = Regex.Replace(data, @"<(.|\n)*?>", string.Empty);
            MatchCollection DataWords = Regex.Matches(data, @"\w*");
            Int32 TotalWords = DataWords.Count;
            String NewString = "";
            foreach (Match m in DataWords)
            {
                if (m.Index >= MaxLength)
                {
                    NewString += "...";
                    break;
                }
                if (m.Value == "")
                {
                    if (m.Index < data.Length)
                        NewString += data.Substring(m.Index, 1);
                }
                else
                    NewString += m.Value;
            }
            return NewString;
        }

     

    And called it like:

    <div><%# UtilityFunctions.GetStrippedString(Convert.ToString(Eval("Product_SDesc")), 50)%></div>

     

     

     

    Monday, November 29, 2010 5:00 AM