Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substring Count in Twig

I am looking to use substr_count in Twig, does anything exist already? I want to perform something like this;

<?php
$text = 'This is a test';
echo strlen($text); // 14

echo substr_count($text, 'is'); // 2

I can do an extension but it seems this might be something built in already that I have missed.

like image 804
Jake N Avatar asked Jul 04 '16 09:07

Jake N


1 Answers

How about this?

{%set count = text|split('is')|length-1 %}
like image 193
Russian developer Avatar answered Sep 26 '22 06:09

Russian developer