I am exporting some fields to an XML feed. There is a rich text field with typically 4 paragraphs in it.

The default export kicks out:

<p>Some text</p> tags as well as stuff like <strong>.

This is easy to remove with |striptags.

However, this results in text that doesn't have proper spacing and removes any intended carriage returns.

I can fix the spacing with a

|replace(".", ". ")

But I'd rather have the the Paragraphs to still have carriage returns as intended. I know I can add in the right UTF-8 symbol, but I don't want this in the data, I just want the spacing and carriage returns as if it were a plain text field and not to have the UTF-8 symbols in the feed itself.


Any help, much appreciated.

1 Answer 1


Completely untested, but you should be able to do use something like the Craft-TwigPCRE plugin and some variation of this code:

{% set text = "<p>Some text</p> tags as well as stuff like <strong>.|preg_replace('/\<p\>/', '') %}

{% set text = text|preg_replace('/\<\/p\>/', '\\n') %}

\n is the Line Feed control character for Unix/OSX.

  • Many thanks Brad, I will take a look later this afternoon and let you know how I get on.
    – Pathfinder
    Aug 11, 2015 at 12:09
  • Okay, spent some time on this one and ran into several issues. The feed I tested it on is here: flourish-uk.com/indeed2.xml The issues are that without |raw the text comes in with the symbol codes instead, so &lt; etc instead of the actual <p> tags. In addition, while the preg_replace works, it struggles when |raw is enabled - nothing appears! Using preg_replace as a test, I can get it to display the text with the extra symbols - replace them - but \\n appears as just that \\n and not a new line.
    – Pathfinder
    Aug 11, 2015 at 13:41
  • Updated and simplified the XML for now, to show the issues.
    – Pathfinder
    Aug 11, 2015 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.