代码之家  ›  专栏  ›  技术社区  ›  My Other Me

是否有类似于ASP.NET MVC的PHP函数nl2br()的功能?

  •  6
  • My Other Me  · 技术社区  · 14 年前

    有没有做换行的股票标准功能 <br /> 在ASP.NETMVC中编码?

    5 回复  |  直到 14 年前
        1
  •  11
  •   Daniel Renshaw    14 年前

    现在有:

    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");
        }
    }
    

    修改为更紧密地遵循nl2br的php规范(感谢Max指出这一点)。这仍然假定\r\n新的行。。。

        2
  •  5
  •   Residuum    14 年前

    所有这些答案都很正确,但你应该做一个调查 mystring.Replace("\r?\n", "<br />"); 如果您的源代码(用户输入或db)可能提供UNIX行结束符,那么也可以捕获UNIX行结束符。

        3
  •  3
  •   Jamie Dixon    14 年前

    myString.Replace("\r\n", "<br />");
    
        4
  •  2
  •   David    14 年前
    mystring.Replace("\r\n","<br />");
    
        5
  •  1
  •   webbiedave    14 年前

    我们可以这样做:

    public static string Nl2br(string str)
    {
        return Nl2br(str, true);
    }
    
    public static string Nl2br(string str, bool isXHTML)
    {
        string brTag = "<br>";
        if (isXHTML) {
            brTag = "<br />";
        }
        return str.Replace("\r\n", brTag + "\r\n");
    }
    

    字符串nl2br(字符串$string[, bool$is\u xhtml=true])

    PHP函数还在break标记后面追加一个换行符。