是否有像ASP.NET MVC的PHP函数nl2br()?

My *_* Me 6 php asp.net-mvc

是否有<br />在ASP.Net MVC中对编码进行换行的库存标准功能?

Dan*_*haw 10

现在有:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

修改后更仔细地遵循nl2br的php规范(感谢Max指出这一点).这仍然假设\ r \n新线虽然......


Res*_*uum 5

所有这些答案都是正确的,但mystring.Replace("\r?\n", "<br />");如果您的源(用户输入或数据库)可以提供,那么您应该执行一次捕获UNIX行结尾.