nl2br

nl2br(string $string, bool $use_xhtml = true): string

Inserts an HTML line-break tag (default <br />, XHTML style) before every newline in a string; no HTML escaping.

C C23

C23
#include <stdio.h>

int main(void) {
    const char *s = "line one\nline two\nend";
    for (const char *p = s; *p != '\0'; p++) {
        if (*p == '\n') {
            printf("<br />\n"); // insert break BEFORE each newline, like nl2br
        } else {
            putchar(*p);
        }
    }
    printf("\n");
    return 0;
}
Output
line one<br />
line two<br />
end

C++ C++26

C++26
#include <iostream>
#include <string>

int main() {
    std::string s = "line one\nline two\nend";
    std::string out;
    for (char ch : s) {
        if (ch == '\n') {
            out += "<br />\n"; // insert break BEFORE each newline, like nl2br
        } else {
            out += ch;
        }
    }
    std::cout << out << "\n";
    return 0;
}
Output
line one<br />
line two<br />
end

C# v11.0.100

v11.0.100
string text = "line one\nline two\nend";
Console.WriteLine(text.Replace("\n", "<br />\n")); // plain replace, no HTML escaping
Output
line one<br />
line two<br />
end

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "strings"
)

func main() {
    text := "line one\nline two\nend"
    fmt.Println(strings.ReplaceAll(text, "\n", "<br />\n")) // html/template auto-escapes instead
}
Output
line one<br />
line two<br />
end

Java v27.0.1

v27.0.1
public class Main {
    public static void main(String[] args) {
        String text = "line one\nline two\nend";
        System.out.println(text.replace("\n", "<br />\n")); // replaceAll needs regex care: "\\n"
    }
}
Output
line one<br />
line two<br />
end

JavaScript v26.13.1

v26.13.1
const text = "line one\nline two\nend";
console.log(text.replaceAll("\n", "<br />\n")); // escape HTML BEFORE this if text is untrusted
Output
line one<br />
line two<br />
end

PHP v8.6.0

v8.6.0
echo nl2br("line one\nline two\nend"); // <br /> goes BEFORE each \n; 2nd arg false gives <br>
Output
line one<br />
line two<br />
end

Python v3.15.2

v3.15.2
text = "line one\nline two\nend"
print(text.replace("\n", "<br />\n"))  # plain replace, no HTML escaping
Output
line one<br />
line two<br />
end

Rust v1.99.1

v1.99.1
fn main() {
    let text = "line one\nline two\nend";
    println!("{}", text.replace("\n", "<br />\n")); // str::replace, no HTML escaping
}
Output
line one<br />
line two<br />
end

TypeScript v26.13.1

v26.13.1
const text: string = "line one\nline two\nend";
console.log(text.replaceAll("\n", "<br />\n")); // escape HTML BEFORE this if text is untrusted
Output
line one<br />
line two<br />
end

Language differences

C: hand-writes a char-by-char copy+insert, equally unescaping.

C++: hand-writes a char-by-char copy+insert, equally unescaping.

C#: built-in replace for the plain \n → <br />\n substitution, equally unescaping.

Go: built-in replace; html/template auto-escapes every interpolation (keeping a literal <br /> requires the template.HTML type), while text/template does not.

Java: built-in replace; Java EE/JSP template engines follow the same default-escape logic.

JavaScript: uses replaceAll; the XSS context is the point of this entry — feeding the replaced string into innerHTML is the classic injection vector: escape HTML entities (& < > " ') first for untrusted text, or skip innerHTML entirely (textContent plus CSS white-space: pre-line lets the browser render \n itself, zero injection surface); "escape first, insert br last" is the universal safe order.

PHP: core semantics — nl2br inserts rather than replaces: <br /> goes before each \n and the newline itself is kept (so HTML source has both the br and the newline; nothing is lost in <pre> or plain text); the default tag is XHTML style <br />, pass false as the second argument for <br>; PHP does not escape HTML: untrusted input must be escaped first with htmlspecialchars($s, ENT_QUOTES) and only then passed to nl2br() — doing it in reverse means <script> already made it in; newline normalization: Windows \r\n becomes <br />\r\n, so normalize with a replace of \r\n to \n before rendering.

Python: built-in replace for the plain substitution, equally unescaping; Django/Jinja2 template engines follow the same default-escape logic.

Rust: built-in replace; askama and other template engines follow the same default-escape logic.

TypeScript: same as JS — replaceAll.