<p>In my opinion, the first thing you should do is <strong>remove all HTML tags</strong>. It's not just paragraph tags which could cause you problems... what about <code>&lt;strong&gt;</code>, <code>&lt;a&gt;</code> or <code>&lt;img&gt;</code> tags which get cut off?</p>

<p>For legibility sake, let's also break this down into multiple steps:</p>

<pre><code>{% set textBlock = entry.bodyContent.type('text').first() %}
{% set plainText = textBlock.text|striptags %}
{% set sumtext   = plainText|hacksaw(chars='100', append='...') %}
</code></pre>

<p>And finally, if you <strong>do</strong> want your text wrapped in a paragraph tag, just reapply that when you output it:</p>

<pre><code>&lt;p&gt;{{ sumtext }}&lt;/p&gt;
</code></pre>

<p>Since you mentioned having an issue with two paragraphs getting mushed together, let me address that too... You'll want to make use of the <a href="http://buildwithcraft.com/docs/templating/filters#replace" rel="nofollow">Craft <code>replace</code> filter</a>, which can handle regex.</p>

<p>Couple of ways you could tackle that:</p>

<ul>
<li><code>{% set plainText = plainText|replace('/\. */', '. ') %}</code> (ensures periods are followed by a single space)</li>
<li><code>{% set textBlock = textBlock|replace('/&lt;\/p&gt; *&lt;p&gt;/', ' ') %}</code> (gracefully combines paragraphs with a space in between)</li>
</ul>

CC BY-SA 3.0
2015-05-12T01:55:19.697Z
Craft's replace filter is masked by Twig's replace filter, and the net result is no regex: [bug report](http://feedback.buildwithcraft.com/forums/285224-bugs/suggestions/7770024-twig-replace-filter-is-masking-craft-replace-filte)
Marion Newlevant
CC BY-SA 3.0
2015-05-12T09:31:01.717Z
Ok - so while this bug exists I won't be able to use the 2 reg ex replace options you displayed above?
mmc501
CC BY-SA 3.0
2015-05-12T16:06:30.787Z
[http://craftcms.stackexchange.com/a/9517/130](http://craftcms.stackexchange.com/a/9517/130) has some options
Marion Newlevant
CC BY-SA 3.0
2015-05-12T17:17:33.120Z
You can also check out the [**Low Regex** filter...](https://github.com/low/low_regex) It's no longer supported, since Craft (normally) has this rolled into the core. But since the core version currently has a bug, you can probably still use Low Regex instead.
Lindsey D
CC BY-SA 3.0
2015-05-11T21:23:51.280Z
2015-05-11T21:23:51.280Z
Lindsey D
<p>I want to display first 100 characters from a rich text field.</p>

<p>I tried this:</p>

<pre><code>{% set sumtext =  entry.bodyContent.type('text').first().text|hacksaw(chars='100', append='...') %}
</code></pre>

<p>But if there was a new paragraph in the first 100 characters and no full stop - both paragraphs are just joined together without a space.</p>

<p>I then tried:</p>

<pre><code>{% set sumtext =  entry.bodyContent.type('text').first().text|hacksaw(allow='&lt;p&gt;', chars='100', append='...') %}
</code></pre>

<p>But the <p> gets displayed on the screen instead of the new line.</p>

<p>what is my best method to display the first 100 characters from a rich text field?</p>

<plugins><filter>
hacksaw plugin - displaying first 100 characters
# hacksaw plugin - displaying first 100 characters

(Asked by: username_0 on 2015-05-11)

I want to display first 100 characters from a rich text field.
I tried this:
{% set sumtext =  entry.bodyContent.type('text').first().text|hacksaw(chars='100', append='...') %}

But if there was a new paragraph in the first 100 characters and no full stop - both paragraphs are just joined together without a space.
I then tried:
{% set sumtext =  entry.bodyContent.type('text').first().text|hacksaw(allow='', chars='100', append='...') %}

But the  gets displayed on the screen instead of the new line.
what is my best method to display the first 100 characters from a rich text field?

**Tags:** plugins, filter

---

## 1 answers

### Answer by username_1 on 2015-05-11. Score: 4

In my opinion, the first thing you should do is remove all HTML tags. It's not just paragraph tags which could cause you problems... what about ,  or  tags which get cut off?
For legibility sake, let's also break this down into multiple steps:
{% set textBlock = entry.bodyContent.type('text').first() %}
{% set plainText = textBlock.text|striptags %}
{% set sumtext   = plainText|hacksaw(chars='100', append='...') %}

And finally, if you do want your text wrapped in a paragraph tag, just reapply that when you output it:
{{ sumtext }}

Since you mentioned having an issue with two paragraphs getting mushed together, let me address that too... You'll want to make use of the Craft replace filter, which can handle regex.
Couple of ways you could tackle that:

{% set plainText = plainText|replace('/\. */', '. ') %} (ensures periods are followed by a single space)
{% set textBlock = textBlock|replace('/<\/p> */', ' ') %} (gracefully combines paragraphs with a space in between)

> **Comments:**
> **username_2**: Craft's replace filter is masked by Twig's replace filter, and the net result is no regex: [bug report](http://feedback.buildwithcraft.com/forums/285224-bugs/suggestions/7770024-twig-replace-filter-is-masking-craft-replace-filte)  
> **username_0**: Ok - so while this bug exists I won't be able to use the 2 reg ex replace options you displayed above?  
> **username_2**: [http://craftcms.stackexchange.com/a/9517/130](http://craftcms.stackexchange.com/a/9517/130) has some options  
> **username_1**: You can also check out the [**Low Regex** filter...](https://github.com/low/low_regex) It's no longer supported, since Craft (normally) has this rolled into the core. But since the core version currently has a bug, you can probably still use Low Regex instead.  

---

