Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    May 06, 2011 @ 08:17
    Tom
    0

    Read More with regex

    Hi Guys,

    I was wanting to craft a read more link using regex where it would cut the text at the nearest word with the entire string being less than X number of characters..

     

    i was wondering how I'd use the following expression in xslt to get the right outcome:

    (.{200}.*?)\b
  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    May 06, 2011 @ 08:40
    Dirk De Grave
    1

    Tom,

    I'd go with an xslt extension and write a static method which does what it needs to do, limiting text to 200 chars... Anyway, need to be aware that you'll have to parse the text as well, as it may contain links and html tags (if that's the case for you...).

    Here's some code that may get you started (it's code taken from xslt search, so credits go to @drobar):

    public string StripTagsRegex(string source) { 
      return Regex.Replace(source, "<.*?>", string.Empty); 
    }

    Method will strip any html tags. And after that, make sure check for incomplete words (after limiting to 200 chars). Can use following code snippet for that:

    public string StripUntilSpace(string source) {
      string copyOfSource = source;
      try {
        while (copyOfSource.Substring(copyOfSource.Length - 1, 1) != " ") {
          copyOfSource = copyOfSource.Substring(0, copyOfSource.Length - 1);
        }
        return copyOfSource;
      } catch { return source; }
      return source;
    }

    You can also use these functions inline in your xslt code, but there could be some performance issues when doing do.

    Hope this helps.

    Regards,

    /Dirk

     

     

     

  • Tom 713 posts 954 karma points
    May 06, 2011 @ 08:50
    Tom
    0

    Hi Dirk! excellent thanks so much! looks like exactly what i need! :)

Please Sign in or register to post replies

Write your reply to:

Draft