We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30271
    • 19 Posts
    Hello,

    I’m looking for a solution for cuting a text after a number of words with getResources.

    I find a solution to limit x types in the chunk
    [[*content:limit=`65`]]

    but it isn’t so good when a word is cuting.

    If you have a solution, it will be a big help.




      • 11076
      • 159 Posts
      Maybe this snippet i once wrote can be an assistance
      it’s simply trim a text to a certain word length and add ellipses (...) if needed & desired (optional).

      <?php
      /*
       * Trim a text to a certain word length and add ellipses (...) if needed & desired (optional).
       * $str - is a string with the text to trim.
       * $length - is an int length of how many words to use.
       * $ellipses - 'yes' (optional), adds '...'.
       *
       * Stracture:
       * [[!TrimWords? &str= &length= &ellipses=yes]] 
       *
       * Usage Example:
       * [[!TrimWords? &str='This is my string' &length=2 &ellipses=yes]] output will be: "This is...".
       */
      
      $output = '';
      
      if (isset($str, $length)) {  
      	if (str_word_count($str,0) > $length) {
      		$output = implode(' ',array_slice(str_word_count($str,1),0,$length));
      		if ($ellipses == 'yes') $output .= '...';
      	}
      	else $output=$str;
      }
      return $output;
      ?>


      Take in mind that it limits by the amount of words chosen over the length parameter.
      if you want to restrict it by amount of chars it’s need to be modified,

      over the sample below,
      $length is an int of how many chars we want to limit.
      it would end with the last whole word on the limited chars string.
      <?php
      
      $output = '';
      
      if (isset($str, $length)) { 
      	if (strlen($str) > $length) {
      		$output = substr($str, 0, strrpos(substr($str, 0, $length), ' '));
      		if ($ellipses == 'yes') $output .= '...';
      	}
      	else $output=$str;
      }
      return $output;
      
      ?>



      you can also combine both to one snippet filter smiley

      another info that might help you:
      http://rtfm.modx.com//display/revolution20/Input+and+Output+Filters

      it’s a little bit strange that MODx doesn’t have it as a built-in filter, maybe they thought that the word warp & limit filters are enough.
        Michael Shraibman( gOmp)  | Freelance Design & Development | wink  impossible is nothing...
        • 30271
        • 19 Posts
        Thank you so much for your help. I read yesterday your first answer (before it was modified) and I tested it. I found like you that it’s better to limited the number of chars and display only words that are integers... so, I’ve tried to modify your code and finally I’ve found a small php code and made a Snippet
        <?php
        //Snippet name: limitWords
        //Required properties: &chaine (string) - &max (number of chars)
        //Example: [[!limite? &chaine=`[[*longtitle]]` &max=`50`]]
        if (isset($chaine, $max)){
         if (strlen($chaine) >= $max) {
         $chaine = substr($chaine, 0, $max);
         $espace = strrpos($chaine, " ");
         $chaine = substr($chaine, 0, $espace)."...";
        }
        }
        echo $chaine;
        ?>


        Call of the snippet in the getResources chunk:
        [[!limitWords? &chaine=`[[+description]]` &max=`90`]]


        I think your solution is better.
          • 11076
          • 159 Posts
          glad to hear it helped you wink
          it’s practically almost the same, except the different var names (you can change them to yours) that are minimized to one single line. and made ellipses optional.

          if (strlen($chaine) >= $max) {[/QUOTE]
          The Equal (=) Comparison Operator is not needed, it will cause an ellipses (...) even when the text is fine in the first place.

          umm..
          i think it’s maybe better to use ’return’ instead of outputting with an ’echo’, but it’s matter of choise.
          http://modxcms.com/forums/index.php/topic,21467.0.html

          that’s all smiley
            Michael Shraibman( gOmp)  | Freelance Design & Development | wink  impossible is nothing...
            • 30271
            • 19 Posts
            Thank you very much, I will do that.
              • 9536
              • 0 Posts
              I noticed this problem:
              This code cuts text within the HTML tags. So I'm getting invalid code.
              Any solution?

              Thanks for your support
              Florian
                • 40906
                • 23 Posts
                Not sure if this is of any help anymore but i fixed it by using this:

                [[+tv.yourtvname:ellipsis=`300`:strip_tags=`<br/>`]]


                It creates a and at 300 characters and puts ... behind the last word.