Answers.Body: <p>If you need to allow or disallow specific tags, you can use wordsmith plugin and its <a href="https://wordsmith.docs.topshelfcraft.com/guide/truncation.html#chop" rel="nofollow noreferrer">chop</a> filter.</p>
  
  <p>But if you don't need such advanced functionality, you can just use Twig macros.</p>
  
  <p>In article <a href="http://craftsnippets.com/articles/truncating-text-with-twig-macros-in-craft-cms?s=s" rel="nofollow noreferrer">Truncating text with Twig macros in Craft CMS
  </a> on my blog I describe two macros - one that truncates by <strong>number of words</strong> and one that truncates by <strong>number of characters</strong> - while ensuring that truncated string does not have split words.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-11-26T11:32:19.360Z
Answers.Text: In 2022 this is a better solution, marked it as the new correct answer given it references Wordsmith as well as alternate methods.
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T01:51:47.213Z
Answers.LastActivityDate: 2019-07-11T19:36:12.347Z
Answers.LastEditDate: 2019-07-11T19:36:12.347Z
Answers.UserDisplayName: Piotr Pogorzelski
Answers.Body: <p><a href="https://github.com/ryanshrum/hacksaw" rel="nofollow noreferrer">https://github.com/ryanshrum/hacksaw</a></p>
  
  <p>This hasn't been fully tested for production, or documented for that matter, but I have migrated it over and tested <strong>basic</strong> usage as per the docs on devot-ee. It should, at the very least, give you a nice start.</p>
  
  <p>You can use the Twig filter with: <code>{{ entry.fieldHandle|hacksaw(words="100") }}</code></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-01T21:20:59.540Z
Answers.Text: Rock on Ryan, thanks for porting it over. Get it up on http://straightupcraft.com/ when you get a chance. This does exactly what i need (as far as my current needs), haven't tested it extensively.
Answers.UserDisplayName: Christopher Healey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-01T21:44:42.843Z
Answers.Text: After posting it originally, I went back and modified a few things after testing it a bit more thoroughly. I noticed one of the downfalls was as you said leaving HTML elements unclosed so I added in a new method to take care of that. I believe it now to function as the EE version does. I will work at getting it up on Straight up Craft as well as getting it fully documented here in the next couple days. Glad to help.
Answers.UserDisplayName: Ryan Shrum
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-21T11:45:27.123Z
Answers.Text: Nice! tnx for sharing.
Answers.UserDisplayName: Martijn La Feber
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-07-21T15:25:48.880Z
Answers.Text: We see an issue with current version of Hacksaw where the content contains non-breaking spaces. They're being converted into little black diamonds with questions marks in them. Redactor loves to add non-breaking spaces, in my experience.
Answers.UserDisplayName: Ryan Masuga
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-01T20:55:10.150Z
Answers.Text: There's also another issue that it's not working currently well with text in Cyrillic (or maybe any Unicode enabled range of characters).
Answers.UserDisplayName: certainlyakey
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-12-29T22:02:02.250Z
Answers.Text: I installed it and all I see now is `Parse error: syntax error, unexpected '[' in F:\xampp\htdocs\craft\plugins\hacksaw\twigextensions\HacksawTwigExtension.php on line 32` and I can't even get back to uninstalling the plugin.
Answers.UserDisplayName: Manticore
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-01T20:09:21.760Z
Answers.LastActivityDate: 2016-11-02T21:11:11.110Z
Answers.LastEditDate: 2016-11-02T21:11:11.110Z
Answers.UserDisplayName: Ryan Shrum
Answers.Body: <p>Doesn't answer the specific question but based on the title in search results it might be helpful for anyone just wanting to truncate a string:</p>
  
  <pre><code>{{ entry.title | length &gt; 75 ? entry.title | slice(0, 75) ~ '...' : entry.title }}
  </code></pre>
  
  <p>Also doesn't require a plugin. Based on <a href="https://stackoverflow.com/questions/13143001/symfony-2-twig-limit-the-length-of-the-text-and-put-three-dots">this</a>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-09T10:21:15.040Z
Answers.LastActivityDate: 2016-11-09T11:33:08.203Z
Answers.LastEditDate: 2017-05-23T12:38:53.957Z
Answers.UserDisplayName: Clive Portman
Answers.Body: <p><a href="https://wordsmith.docs.topshelfcraft.com/guide/truncation.html" rel="nofollow noreferrer">Wordsmith</a> plugin is very useful in manipulating the text. For example:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ entry.fieldHandle|chop(limit=100, unit='c', append=' ...') }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-09T01:18:08.177Z
Answers.LastActivityDate: 2020-06-09T01:18:08.177Z
Answers.UserDisplayName: Alex Ljamin
Body: <p>I see that there are <a href="https://github.com/rkingon/Craft-Plugin--Trimmer">Trimmer</a> and <a href="https://github.com/nizur/truncate/">Truncate</a> out there that have been created for truncating text but I don't believe either does what it is I'm looking for.</p>
  
  <p>I'd like to truncate text similar to how <a href="http://devot-ee.com/add-ons/hacksaw">Hacksaw</a> for ExpressionEngine works. Allowing me to allow or disallow html tags, and making sure that even if it cuts off before a tag is closed, that it automatically properly closes the tag. I wouldn't want to turn the entire lower half of my page into a hyperlink for instance if i happen to truncate mid-link. I do want to maintain the ability for links to be in the text that is being truncated, and multiple paragraphs, etc. however.</p>
  
  <p>Any tips would be fantastic.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-01T19:12:49.930Z
Comments.Text: [Hacksaw](http://devot-ee.com/add-ons/hacksaw) has an open source license (MIT), so you could make a Craft version of it.
Comments.UserDisplayName: Marion Newlevant
Tags: <templating><richtext>
Title: Best method of text truncating
ThreadText: # Best method of text truncating
  
  (Asked by: username_0 on 2014-07-01)
  
  I see that there are [Trimmer](https://github.com/rkingon/Craft-Plugin--Trimmer) and [Truncate](https://github.com/nizur/truncate/) out there that have been created for truncating text but I don't believe either does what it is I'm looking for.
  
  I'd like to truncate text similar to how [Hacksaw](http://devot-ee.com/add-ons/hacksaw) for ExpressionEngine works. Allowing me to allow or disallow html tags, and making sure that even if it cuts off before a tag is closed, that it automatically properly closes the tag. I wouldn't want to turn the entire lower half of my page into a hyperlink for instance if i happen to truncate mid-link. I do want to maintain the ability for links to be in the text that is being truncated, and multiple paragraphs, etc. however.
  
  Any tips would be fantastic.
  
  **Tags:** templating, richtext
  
  > **Comments:**
  > **username_1**: [Hacksaw](http://devot-ee.com/add-ons/hacksaw) has an open source license (MIT), so you could make a Craft version of it.  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_2 on 2019-01-08. Score: 2 | Accepted answer
  
  If you need to allow or disallow specific tags, you can use wordsmith plugin and its [chop](https://wordsmith.docs.topshelfcraft.com/guide/truncation.html#chop) filter.
  
  But if you don't need such advanced functionality, you can just use Twig macros.
  
  In article [Truncating text with Twig macros in Craft CMS](http://craftsnippets.com/articles/truncating-text-with-twig-macros-in-craft-cms?s=s) on my blog I describe two macros - one that truncates by **number of words** and one that truncates by **number of characters** - while ensuring that truncated string does not have split words.
  
  > **Comments:**
  > **username_0**: In 2022 this is a better solution, marked it as the new correct answer given it references Wordsmith as well as alternate methods.  
  
  ---
  
  ### Answer by username_3 on 2014-07-01. Score: 18
  
  <https://github.com/username_3/hacksaw>
  
  This hasn't been fully tested for production, or documented for that matter, but I have migrated it over and tested **basic** usage as per the docs on devot-ee. It should, at the very least, give you a nice start.
  
  You can use the Twig filter with: `{{ entry.fieldHandle|hacksaw(words="100") }}`
  
  > **Comments:**
  > **username_0**: Rock on username_3, thanks for porting it over. Get it up on http://straightupcraft.com/ when you get a chance. This does exactly what i need (as far as my current needs), haven't tested it extensively.  
  > **username_3**: After posting it originally, I went back and modified a few things after testing it a bit more thoroughly. I noticed one of the downfalls was as you said leaving HTML elements unclosed so I added in a new method to take care of that. I believe it now to function as the EE version does. I will work at getting it up on Straight up Craft as well as getting it fully documented here in the next couple days. Glad to help.  
  > **username_4**: Nice! tnx for sharing.  
  > **username_5**: We see an issue with current version of Hacksaw where the content contains non-breaking spaces. They're being converted into little black diamonds with questions marks in them. Redactor loves to add non-breaking spaces, in my experience.  
  > **username_6**: There's also another issue that it's not working currently well with text in Cyrillic (or maybe any Unicode enabled range of characters).  
  > **username_7**: I installed it and all I see now is `Parse error: syntax error, unexpected '[' in F:\xampp\htdocs\craft\plugins\hacksaw\twigextensions\HacksawTwigExtension.php on line 32` and I can't even get back to uninstalling the plugin.  
  
  ---
  
  ### Answer by username_8 on 2016-11-09. Score: 13
  
  Doesn't answer the specific question but based on the title in search results it might be helpful for anyone just wanting to truncate a string:
  
  ```
  {{ entry.title | length > 75 ? entry.title | slice(0, 75) ~ '...' : entry.title }}
  
  ```
  
  Also doesn't require a plugin. Based on [this](https://stackoverflow.com/questions/13143001/symfony-2-twig-limit-the-length-of-the-text-and-put-three-dots).
  
  
  ---
  
  ### Answer by username_9 on 2020-06-09. Score: 2
  
  [Wordsmith](https://wordsmith.docs.topshelfcraft.com/guide/truncation.html) plugin is very useful in manipulating the text. For example:
  
  ```twig
  {{ entry.fieldHandle|chop(limit=100, unit='c', append=' ...') }}
  
  ```
  
  
  ---
  
  