Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something like the PHP function nl2br() for ASP.NET MVC?

Is there stock standard function that does newline to <br /> encoding in ASP.Net MVC?

like image 478
My Other Me Avatar asked Apr 29 '10 14:04

My Other Me


1 Answers

There is now:

public static class StockStandardFunctions
{
    public static string Nl2br(this string input)
    {
        return input.Nl2br(true);
    }

    public static string Nl2br(this string input, bool is_xhtml)
    {
        return input.Replace("\r\n", is_xhtml ? "<br />\r\n" : "<br>\r\n");
    }
}

Amended to follow the php spec for nl2br a little more closely (thanks Max for pointing that out). This still assumes \r\n new lines though...

like image 80
Daniel Renshaw Avatar answered Nov 09 '22 08:11

Daniel Renshaw