<p>For what it's worth, you can strip out all non-numerics with this something like this:</p>

<pre><code>{{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }}
</code></pre>

<p>Or, as part of a tel: anchor:</p>

<pre><code>&lt;a href="tel:{{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }}"&gt;{{ globalPhoneNumber.phoneNumber }}&lt;/a&gt;
</code></pre>

CC BY-SA 3.0
2016-05-26T19:27:18.650Z
2016-05-26T19:27:18.650Z
Nick Benson
<p>I haven't tried this but I think that you can use the twig <a href="http://twig.sensiolabs.org/doc/filters/replace.html" rel="nofollow">replace</a> function. Something like:</p>

<pre><code>{{ global.phone|replace({"(":"", ")":"", "-":"", " ":""}) }}
</code></pre>

<p><strong>Update</strong> In your case, since the prefix (1-) is required, and "-" are allowable, the replace string for "(555) 555-5555" would be:</p>

<pre><code>{{ global.phone|replace({"(" : "1-", ") " : "-"}) }}
</code></pre>

CC BY-SA 3.0
2014-11-12T16:45:22.057Z
Works great, however got an error with double pipe. Left one in code and it works no problem. Thank you!
frshjb373
CC BY-SA 3.0
2014-11-12T16:45:56.810Z
I fixed that. Sorry about that.
Douglas McDonald
CC BY-SA 3.0
2014-11-12T16:47:02.570Z
I'm glad it works. Which reminds me... I need to do that for a project I'm working on. So thanks! ;)
Douglas McDonald
CC BY-SA 3.0
2014-11-12T16:52:46.123Z
fyi... i think that the phone number needs to include the prefix (1-) and dashes are allowable (i.e. 1-555-555-5555). So you may need to prefix that number as well.
Douglas McDonald
CC BY-SA 3.0
2014-11-12T17:04:59.177Z
I'll use the latter, but looking at documentation and recommendations around web, not seeing a clear rule to use (1-) prefix or not. Same goes for including dashes or not. I've always done only numbers disregarding prefix and that's always worked for me.
frshjb373
CC BY-SA 3.0
2014-11-12T19:20:00.350Z
Check out [this apple dev page](https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html). Looks like "+1" is the preferred prefix. The hyphens are completely optional.
Douglas McDonald
CC BY-SA 3.0
2014-11-12T16:43:21.410Z
2014-11-12T16:57:52.410Z
2014-11-12T16:57:52.410Z
Douglas McDonald
<p>I am using a phone number in a global variable called <code>{{ global.phone }}</code> with the format "(555) 555-5555". I'd like to use that same variable/string and remove spaces, dash, and parenthesis to use for the href tel link. I've tried a few different things from Twig and Craft documentation, but nothing is working. Here's what I have right now.</p>

<pre><code>&lt;p&gt;Ph: &lt;a href="tel:{{ global.phone|trim(' ') }}"&gt;{{ global.phone }}&lt;/a&gt;&lt;br/&gt;
</code></pre>

<p>Is this possible to do? If so any advise how to accomplish this?</p>

<templating><variable><string><globals>
Remove white space and parenthsis from global from entry variable
# Remove white space and parenthsis from global from entry variable

(Asked by: username_0 on 2014-11-12)

I am using a phone number in a global variable called {{ global.phone }} with the format "(555) 555-5555". I'd like to use that same variable/string and remove spaces, dash, and parenthesis to use for the href tel link. I've tried a few different things from Twig and Craft documentation, but nothing is working. Here's what I have right now.
Ph: {{ global.phone }}

Is this possible to do? If so any advise how to accomplish this?

**Tags:** templating, variable, string, globals

---

## 2 answers

### Answer by username_1 on 2016-05-26. Score: 4

For what it's worth, you can strip out all non-numerics with this something like this:
{{ globalPhoneNumber.phoneNumber|replace('/[^0-9]/', '') }}

Or, as part of a tel: anchor:
{{ globalPhoneNumber.phoneNumber }}


---

### Answer by username_2 on 2014-11-12. Score: 4

I haven't tried this but I think that you can use the twig replace function. Something like:
{{ global.phone|replace({"(":"", ")":"", "-":"", " ":""}) }}

Update In your case, since the prefix (1-) is required, and "-" are allowable, the replace string for "(555) 555-5555" would be:
{{ global.phone|replace({"(" : "1-", ") " : "-"}) }}

> **Comments:**
> **username_0**: Works great, however got an error with double pipe. Left one in code and it works no problem. Thank you!  
> **username_2**: I fixed that. Sorry about that.  
> **username_2**: I'm glad it works. Which reminds me... I need to do that for a project I'm working on. So thanks! ;)  
> **username_2**: fyi... i think that the phone number needs to include the prefix (1-) and dashes are allowable (i.e. 1-555-555-5555). So you may need to prefix that number as well.  
> **username_0**: I'll use the latter, but looking at documentation and recommendations around web, not seeing a clear rule to use (1-) prefix or not. Same goes for including dashes or not. I've always done only numbers disregarding prefix and that's always worked for me.  
> **username_2**: Check out [this apple dev page](https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/PhoneLinks/PhoneLinks.html). Looks like "+1" is the preferred prefix. The hyphens are completely optional.  

---

