Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 936 posts 2571 karma points
    Apr 14, 2014 @ 14:52
    Claushingebjerg
    0

    substring html encoded string

    Im trying to do at substring of my RTE content. I wnat the firs xxx characters. No problem. But seeing as the RTE content is html i want to strip the HTML. Got that too. but then i want to display html encoded chracters as normal chracters. that kinda works, but the substring routine cuts the encoded string and, leaves chunks of the encoding... im doing ti likes this

    @Html.Raw(@Umbraco.StripHtml(@childPage.bodyText).ToString().Substring(0, 70))

    ut in cases it leaves me something like this

    Strygeinstrumenter er meget alsidige og bruges i alle genrer b&arin


    How do i avoid this?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 15:00
  • Claushingebjerg 936 posts 2571 karma points
    Apr 14, 2014 @ 15:15
    Claushingebjerg
    0

    That could be a solution... How would a stupid frontender like me go about using it?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 15:28
    Jeavon Leopold
    100

    It would be great if that project would become a package one day :-)

    Anyhow, within the View, you could do like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = "SiteMasterView.cshtml";
        var childpage = CurrentPage;
    }
    <section id="page-content">
    
        @Html.Raw(TruncateAtWord(Umbraco.StripHtml(childpage.bodyText).ToString(),70))
    
    </section>            
    
    @functions
    {
        public static string TruncateAtWord(string textToTruncate, int length)
        {
            if (textToTruncate == null || textToTruncate.Length < length)
                return textToTruncate;
            int iNextSpace = textToTruncate.LastIndexOf(" ", length);
            return string.Format("{0}...", textToTruncate.Substring(0, (iNextSpace > 0) ? iNextSpace : length).Trim());
        }
    }
    
  • Claushingebjerg 936 posts 2571 karma points
    Apr 14, 2014 @ 15:32
    Claushingebjerg
    0

    But wouldnt i need to install those 300+ lines of code that Warren somewhere? 

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 15:33
    Jeavon Leopold
    0

    Nope, this is standalone, just using the method as a Razor function

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Apr 14, 2014 @ 17:33
    Jeavon Leopold
    0

    Did it work?

  • Claushingebjerg 936 posts 2571 karma points
    Apr 15, 2014 @ 08:23
    Claushingebjerg
    0

    Hi Jeavon

    I had to prioritize other matters, so i havent got to test it yet

  • Claushingebjerg 936 posts 2571 karma points
    Jun 30, 2016 @ 07:31
    Claushingebjerg
    0

    A couple of years later... It worked perfectly, thanks Jeavon

Please Sign in or register to post replies

Write your reply to:

Draft