<p>Assuming that you always want to add a space after every two digits, something like this using Twig's <a href="http://twig.sensiolabs.org/doc/filters/split.html" rel="noreferrer">split</a> and <a href="http://twig.sensiolabs.org/doc/filters/join.html" rel="noreferrer">join</a> filters should work:</p>

<pre><code>{% set splitArray = "20304050"|split('', 2) %}
{% set formattedString = splitArray|join(' ') %}
</code></pre>

<p>If you want a different format, like <code>203 04 050</code>, then you could use <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="noreferrer">slice</a>:</p>

<pre><code>{% set firstSeg = "20404050"|slice(0, 3) %}
{% set secondSeg = "20404050"|slice(3, 2) %}
{% set thirdSeg = "20404050"|slice(5, 3) %}

{% set formattedString = firstSeg ~ ' ' ~ secondSeg ~ ' ' ~ thirdSeg %}
</code></pre>

CC BY-SA 3.0
2014-10-01T20:45:30.610Z
What if we need to split it like this: 203 04 050
Alf Vestre
CC BY-SA 3.0
2014-10-01T21:58:22.080Z
Updated answer.
Brad Bell
CC BY-SA 3.0
2014-10-01T20:25:04.320Z
2014-10-01T21:58:16.557Z
2014-10-01T21:58:16.557Z
Brad Bell
<p>Can an integer be formatted as a telephone number using Twig filters or other methods?</p>

<p>I have looked at the 'number_format' filter but it seems to be limited to controlling the number of decimals and how to delimited thousands.</p>

<p>Example; Integer variable from cms '20304050', output to HTML in twig like this: '20 30 40 50'.</p>

<templating>
Can an integer be formatted as a telephone number using Twig?
# Can an integer be formatted as a telephone number using Twig?

(Asked by: username_0 on 2014-10-01)

Can an integer be formatted as a telephone number using Twig filters or other methods?

I have looked at the 'number\_format' filter but it seems to be limited to controlling the number of decimals and how to delimited thousands.

Example; Integer variable from cms '20304050', output to HTML in twig like this: '20 30 40 50'.

**Tags:** templating


---

## 1 answers

### Answer by username_1 on 2014-10-01. Score: 8 | Accepted answer

Assuming that you always want to add a space after every two digits, something like this using Twig's split and join filters should work:
{% set splitArray = "20304050"|split('', 2) %}
{% set formattedString = splitArray|join(' ') %}

If you want a different format, like 203 04 050, then you could use slice:
{% set firstSeg = "20404050"|slice(0, 3) %}
{% set secondSeg = "20404050"|slice(3, 2) %}
{% set thirdSeg = "20404050"|slice(5, 3) %}

{% set formattedString = firstSeg ~ ' ' ~ secondSeg ~ ' ' ~ thirdSeg %}

> **Comments:**
> **username_0**: What if we need to split it like this: 203 04 050  
> **username_1**: Updated answer.  

---

