DMXzone App Connect Support Product Page


Is there a feature like PHP's nl2br that I can use?

Asked 27 Apr 2018 13:43:18
has this question
27 Apr 2018 13:43:18 Bruce Wilkie posted:
Hi there

I've got some text in a mediumtext field in my database and I'd like to display it on the page with the carriage returns/new lines intact.

At present when I show the text on the page it's all in a single line.

Is there a text formatting option that I can use similar to PHP's nl2br() that will show the new lines in the text and <BR> tags on the web page?

thanks in advance


Replied 28 Apr 2018 12:26:08
28 Apr 2018 12:26:08 Tom Dupre replied:
Replied 30 Apr 2018 08:41:30
30 Apr 2018 08:41:30 Bruce Wilkie replied:
Thanks Tom

Do I need to buy App Connect Medium Editor for this to work?
If so, is there an alternative? Is there a simple command that works just like nl2br() ?

Replied 30 Apr 2018 09:01:38
30 Apr 2018 09:01:38 Bruce Wilkie replied:
I've found a script that's supposed to work:

<script type="text/javascript">
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');

But I don't know how to call this in relation to the text held in the database.

I've added that script to the page and then tried doing this:


but nothing displays on the page.

Any ideas what I'm doing wrong?
Replied 30 Apr 2018 10:13:18
30 Apr 2018 10:13:18 Tom Dupre replied:
Did you try using the dmx-html attribute? You don't need any extra extensions for this.
Replied 30 Apr 2018 10:44:53
30 Apr 2018 10:44:53 Bruce Wilkie replied:
Hi Tom

Yes I did, but it didn't work.

The database field doesn't contain any html. It just contains new lines in the text, which the php function nl2br() converts into html BR tags when displayed on the page.

So is there a way to use the javascript I found (above) to do the same with the data I'm displaying using app connect?

Or do you know another way to do this?

Replied 30 Apr 2018 12:53:02
30 Apr 2018 12:53:02 Tom Dupre replied:
Sorry - I was assuming there were <br> tags in the data.

Hopefully there is a simpler way to do this, but I think this works:

<p dmx-html="yourField.replace(&quot;\n&quot;, &quot;&lt;br&gt;&quot"></p>

Again, it uses the dmx-html attribute, in addition to the 'replace' function. Using the interface, select:
Dynamic Attributes - Inner HTML - and use the value yourField.replace("\n", "<br>" (via Data Formats)

Replied 01 May 2018 06:14:17
01 May 2018 06:14:17 Teodor Kuduschiev replied:
Hi Guys,
There are 3 ways of doing this with app connect:

- Use replace(/\r\n|\r|\n/g, '<br>') with the dmx-html attribute (as the replace won't work without this attribute)
- Put your text in a <pre> tag
- Do this using CSS with the white-space: pre; property.
Replied 01 May 2018 14:08:39
01 May 2018 14:08:39 Bruce Wilkie replied:
Thanks Teodor

Put it into a <pre> tag and styled it to match the rest of the text on the page.

Works a charm. Thanks


Reply to this topic