<p>If you're happy relying on a plugin then the <a href="https://plugins.craftcms.com/expressive" rel="nofollow noreferrer">Expressive plugin</a> adds the <code>preg_split</code> function e.g <code>{{ content|preg_split(&quot;/(\?|\.|!)/&quot;) }}</code></p>

CC BY-SA 4.0
2021-06-21T11:10:57.913Z
Almost there but doesn't fix the second part of my question about retaining the delimiter. Unfortunately the plugin doesn't support PREG_SPLIT_DELIM_CAPTURE.
cannon303
CC BY-SA 4.0
2021-06-21T23:11:22.900Z
Make a PR to add an optional $flags parameter to preg_split? https://github.com/IndigoViking/Expressive/blob/master/src/twigextensions/ExpressiveTwigExtension.php#L252-L260
Brad Bell
CC BY-SA 4.0
2022-07-06T21:17:55.637Z
The plugin worked so thanks very much and I've marked it as solved. Unfortunately the Expressive plugin isn't ready for Craft 4 at the time of writing this so even though the plugin has worked for a year I now need to go and find an alternative.
cannon303
CC BY-SA 4.0
2021-06-21T09:46:46.980Z
2021-06-21T09:46:46.980Z
dmatthams
<p>Hi what is the best was to split a paragraph into sentences please?</p>
<p>I can use:</p>
<pre><code>{{ content|split('.') }}
</code></pre>
<p>but what if a sentence in the paragraph ends with a question mark or exclamation mark instead? Such as:</p>
<blockquote>
<p>This is a sentence. This is also a sentence that really interests me!
Why shouldn't this be classed as a sentence? This is also a sentence.</p>
</blockquote>
<p>How can I easily split that string into an array of 4 like this:</p>
<pre><code>[0] This is a sentence
[1] This is also a sentence that really interests me
[2] Why shouldn't this be classed as a sentence 
[3] This is also a sentence
</code></pre>
<p>In php you can use:</p>
<pre><code>$content = preg_split( &quot;/(\?|\.|!)/&quot;, $content ); 
</code></pre>
<p>But this doesn't work with twig.</p>
<p>Also just to make it a bit harder, I would like the strings to keep the characters that are used as delimiters:</p>
<pre><code>[0] This is a sentence. 
[1] This is also a sentence that really interests me! 
[2] Why shouldn't this be classed as a sentence? 
[3] This is also a sentence.
</code></pre>
<p>Any help would be much appreciated. Thanks</p>

<templating>
Split string with multiple delimiters
# Split string with multiple delimiters

(Asked by: username_0 on 2021-06-21)

Hi what is the best was to split a paragraph into sentences please?
I can use:
{{ content|split('.') }}

but what if a sentence in the paragraph ends with a question mark or exclamation mark instead? Such as:

This is a sentence. This is also a sentence that really interests me!
Why shouldn't this be classed as a sentence? This is also a sentence.

How can I easily split that string into an array of 4 like this:
[0] This is a sentence
[1] This is also a sentence that really interests me
[2] Why shouldn't this be classed as a sentence 
[3] This is also a sentence

In php you can use:
$content = preg_split( "/(\?|\.|!)/", $content ); 

But this doesn't work with twig.
Also just to make it a bit harder, I would like the strings to keep the characters that are used as delimiters:
[0] This is a sentence. 
[1] This is also a sentence that really interests me! 
[2] Why shouldn't this be classed as a sentence? 
[3] This is also a sentence.

Any help would be much appreciated. Thanks

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2021-06-21. Score: 2 | Accepted answer

If you're happy relying on a plugin then the [Expressive plugin](https://plugins.craftcms.com/expressive) adds the `preg_split` function e.g `{{ content|preg_split("/(\?|\.|!)/") }}`

> **Comments:**
> **username_0**: Almost there but doesn't fix the second part of my question about retaining the delimiter. Unfortunately the plugin doesn't support PREG_SPLIT_DELIM_CAPTURE.  
> **username_2**: Make a PR to add an optional $flags parameter to preg_split? https://github.com/IndigoViking/Expressive/blob/master/src/twigextensions/ExpressiveTwigExtension.php#L252-L260  
> **username_0**: The plugin worked so thanks very much and I've marked it as solved. Unfortunately the Expressive plugin isn't ready for Craft 4 at the time of writing this so even though the plugin has worked for a year I now need to go and find an alternative.  

---

