learningtech
learningtech

Reputation: 33747

smarty replace line breaks

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.

Upvotes: 6

Views: 10817

Answers (2)

Steve Horvath
Steve Horvath

Reputation: 529

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.

Upvotes: 1

tradyblix
tradyblix

Reputation: 7579

Try this if it works:

{$var|regex_replace:"/[\r\n]/" : " "}

Upvotes: 11

Related Questions