Answers.Body: <p>Here's a different spin... Try removing everything that's <strong>not numeric or comma</strong>.</p>
  
  <pre><code>{{ price|replace('/[^0-9,]/', '') }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-16T01:06:00.307Z
Answers.Text: Ah, that's clever – works like a charm in this specific instance, thanks! However I'd still like to know how to strip out the `&nbsp;` in cases where a greedy regex like that wouldn't work (i.e. when I don't have a numeric string to deal with...)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-16T00:52:30.493Z
Answers.LastActivityDate: 2016-02-16T00:52:30.493Z
Answers.UserDisplayName: Lindsey D
Answers.Body: <p>To add space between two words use below code:</p>
  
  <pre><code>{{ price|replace({" ": "&amp;nbsp;"})|raw  |nl2br }}
  </code></pre>
  
  <p>This will work.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-11T12:47:35.853Z
Answers.LastActivityDate: 2019-12-11T12:52:51.143Z
Answers.LastEditDate: 2019-12-11T12:52:51.143Z
Answers.UserDisplayName: Ash
Answers.Body: <p>If you need to trim a non-breaking space which is the UTF-8 encoded non-breaking space character:</p>
  
  <pre><code>{{ value|trim(" \t\n\r\0\x0B\xC2\xA0") }}
  </code></pre>
  
  <p>This works because internally Twig uses the PHP <code>trim</code>, <code>ltrim</code>, and <code>rtrim functions</code>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-05-15T08:30:32.270Z
Answers.LastActivityDate: 2019-05-15T08:30:32.270Z
Answers.UserDisplayName: Leksat
Answers.Body: <p>You just think it is <code>&amp;nbsp;</code> that get's returned from the filter, because that's how your developer tools display it, but in fact it is a <em>UTF-8 encoded</em> non-breaking space character. Have a look at the source code that is output and you won't see the <code>&amp;nbsp;</code>.</p>
  
  <p>The filter gets the formatting pattern from the <a href="https://github.com/pixelandtonic/Craft-Release/blob/master/app/framework/i18n/data/no.php#L32" rel="nofollow noreferrer">app/framework/i18n/data/no.php</a> file. Everything in this file is UTF-8 encoded and the non-breaking space is no exception.</p>
  
  <p>So how can we remove it? Copy-and-pasting doesn't always work, because it depends on the apps used. On Mac OS X 10.11 I was able to simply copy the string from the source code view in the Safari dev tools to Atom editor and it worked. I can also generate the character with <kbd>Alt</kbd>+<kbd>Space</kbd>. Copying the snippet into this answer doesn't work though, it somehow converts it to a normal space:</p>
  
  <pre><code>{{ price|replace('Nkr ') }}
  </code></pre>
  
  <p><strong>Important sidenote:</strong></p>
  
  <p>Mac OS X users who are like me and sometimes accidentially enter a non-breaking space character into their source code and then go mad trying to fix the error, have a look at this Apple Stack Exchange post: <a href="https://apple.stackexchange.com/questions/34672/whats-altspacebar-character-and-how-to-disable-it">What's alt+spacebar character and how to disable it?</a></p>
  
  <p>Atom editor users can install the <a href="https://atom.io/packages/highlight-nbsp" rel="nofollow noreferrer">highlight-nbsp</a> package.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-16T15:25:02.697Z
Answers.LastActivityDate: 2016-02-16T22:50:08.847Z
Answers.LastEditDate: 2017-04-13T12:45:32.027Z
Answers.UserDisplayName: carlcs
Body: <p>Using Twig, is it possible to strip out or replace a non-breaking space (<code>&amp;nbsp;</code>)?</p>
  
  <p>I've got a string variable <code>price</code> (returned from the <a href="https://craftcms.com/docs/templating/filters#currency" rel="noreferrer"><code>currency</code></a> filter) that prints like this:</p>
  
  <pre><code>Nkr&amp;nbsp;100,00
  </code></pre>
  
  <p>What I'd like, is to strip the <code>Nkr&amp;nbsp;</code> part, so the string looks like this:</p>
  
  <pre><code>100,00
  </code></pre>
  
  <p>However, <code>{{ price|replace('Nkr&amp;nbsp;', '') }}</code> does nothing, as the <code>replace</code> filter doesn't appear to actually match the non-breaking space.</p>
  
  <p><code>{{ price|replace({'Nkr' : ''}) }}</code> strips out the <code>Nkr</code> part, but leaves the <code>&amp;nbsp;</code>; and I'm unable to remove it.  </p>
  
  <p>Here's what I tried already (after stripping the <code>Nkr</code> part):</p>
  
  <pre><code>{{ price|replace({'&amp;nbsp;' : ''}) }}
  {{ price|raw|replace({'&amp;nbsp;' : ''}) }}
  {{ price|striptags }}
  {{ price|raw|striptags }}
  {{ price|trim }}
  {{ price|raw|trim }}
  {{ price|replace({ '\u00a0' : '' }) }}
  {{- price -}} 
  {% spaceless %}{{ price }}{% endspaceless %}
  </code></pre>
  
Tags: <templating><currency>
Title: How can I strip or replace a non-breaking space with Twig?
ThreadText: # How can I strip or replace a non-breaking space with Twig?
  
  (Asked by: username_0 on 2016-02-16)
  
  Using Twig, is it possible to strip out or replace a non-breaking space (`&nbsp;`)?
  
  I've got a string variable `price` (returned from the [`currency`](https://craftcms.com/docs/templating/filters#currency) filter) that prints like this:
  
  ```
  Nkr&nbsp;100,00
  
  ```
  
  What I'd like, is to strip the `Nkr&nbsp;` part, so the string looks like this:
  
  ```
  100,00
  
  ```
  
  However, `{{ price|replace('Nkr&nbsp;', '') }}` does nothing, as the `replace` filter doesn't appear to actually match the non-breaking space.
  
  `{{ price|replace({'Nkr' : ''}) }}` strips out the `Nkr` part, but leaves the `&nbsp;`; and I'm unable to remove it.
  
  Here's what I tried already (after stripping the `Nkr` part):
  
  ```
  {{ price|replace({'&nbsp;' : ''}) }}
  {{ price|raw|replace({'&nbsp;' : ''}) }}
  {{ price|striptags }}
  {{ price|raw|striptags }}
  {{ price|trim }}
  {{ price|raw|trim }}
  {{ price|replace({ '\u00a0' : '' }) }}
  {{- price -}} 
  {% spaceless %}{{ price }}{% endspaceless %}
  
  ```
  
  **Tags:** templating, currency
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_4 on 2016-02-16. Score: 5 | Accepted answer
  
  You just think it is `&nbsp;` that get's returned from the filter, because that's how your developer tools display it, but in fact it is a *UTF-8 encoded* non-breaking space character. Have a look at the source code that is output and you won't see the `&nbsp;`.
  
  The filter gets the formatting pattern from the [app/framework/i18n/data/no.php](https://github.com/pixelandtonic/Craft-Release/blob/master/app/framework/i18n/data/no.php#L32) file. Everything in this file is UTF-8 encoded and the non-breaking space is no exception.
  
  So how can we remove it? Copy-and-pasting doesn't always work, because it depends on the apps used. On Mac OS X 10.11 I was able to simply copy the string from the source code view in the Safari dev tools to Atom editor and it worked. I can also generate the character with `Alt`+`Space`. Copying the snippet into this answer doesn't work though, it somehow converts it to a normal space:
  
  ```
  {{ price|replace('Nkr ') }}
  
  ```
  
  **Important sidenote:**
  
  Mac OS X users who are like me and sometimes accidentially enter a non-breaking space character into their source code and then go mad trying to fix the error, have a look at this Apple Stack Exchange post: [What's alt+spacebar character and how to disable it?](https://apple.stackexchange.com/questions/34672/whats-altspacebar-character-and-how-to-disable-it)
  
  Atom editor users can install the [highlight-nbsp](https://atom.io/packages/highlight-nbsp) package.
  
  
  ---
  
  ### Answer by username_1 on 2016-02-16. Score: 6
  
  Here's a different spin... Try removing everything that's **not numeric or comma**.
  
  ```
  {{ price|replace('/[^0-9,]/', '') }}
  
  ```
  
  > **Comments:**
  > **username_0**: Ah, that's clever – works like a charm in this specific instance, thanks! However I'd still like to know how to strip out the `&nbsp;` in cases where a greedy regex like that wouldn't work (i.e. when I don't have a numeric string to deal with...)  
  
  ---
  
  ### Answer by username_2 on 2019-12-11. Score: 3
  
  To add space between two words use below code:
  
  ```
  {{ price|replace({" ": "&nbsp;"})|raw  |nl2br }}
  
  ```
  
  This will work.
  
  
  ---
  
  ### Answer by username_3 on 2019-05-15. Score: 1
  
  If you need to trim a non-breaking space which is the UTF-8 encoded non-breaking space character:
  
  ```
  {{ value|trim(" \t\n\r\0\x0B\xC2\xA0") }}
  
  ```
  
  This works because internally Twig uses the PHP `trim`, `ltrim`, and `rtrim functions`.
  
  
  ---
  
  