smarty replace line breaks
Asked Answered
E

2

6

At the time of writing this, the smarty.net website appears to be down.

Anyway, how do I replace line breaks with a space in a smarty variable? Is it something like this {$var|regex_replace:'[\\r\\n]':'\s'} ? I tried it but it didn't work.

Entellus answered 21/5, 2011 at 0:27 Comment(2)
Define "it didn't work".Antinucleon
Smarty.net - Oops! Google Chrome could not connect to smarty.net . My regex - linebreaks still existsEntellus
T
11

Try this if it works:

{$var|regex_replace:"/[\r\n]/" : " "}
Tippet answered 21/5, 2011 at 0:34 Comment(1)
It may help some one: In my case there were 2 line breaks (perhaps Paragraph marks), So i find the solution {$var|regex_replace:"/[\r\t\n][\r\t\n]/":""} here:grokbase.com/p/php/php-smarty-general/02bw8zmv2z/…Mottle
U
1

The problem with [\r\n] is that it'll replace a single windows crlf with a double replacement. (this is not a biggie if you just output spaces, but...)

Example:

{$letter="--\n--\r\n--\r\n\r\n--"}
{$var|regex_replace:"/[\r\n]/":"BR"}
result:
--BR--BRBR--BRBRBRBR--

Consider if you want to replace line breaks with html newlines; the above will create a mess. This is what works as expected:

{$var|regex_replace:"/\r*\n/":"<br>"}

(Btw; if you consider nl2br for the above; it won't replace newlines, it'll just add a br to each - that can be a problem in some cases)

Now, the classic mac newline is just a carriage return, so more tweaking would be needed for that, but it's probably not really existent anymore.

Unhandled answered 3/3, 2020 at 22:27 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.