You can remove it by replacing it with nothing,

str_replace("\n","",$row['name']);

If you need your line break to be in your JS string you can double escape it,

str_replace("\n","\\n",$row['name']);
Answer from lostsource on Stack Overflow
🌐
PHPpot
phppot.com › php › php-line-breaks
PHP Line Breaks: \n, PHP_EOL and nl2br() Explained - PHPpot
July 22, 2026 - If you need to display a PHP string with line breaks inside an HTML page, use nl2br(). <?php $message = "First line\nSecond line\nThird line"; echo nl2br($message); ?> The output will display each sentence on a separate line in the browser. PHP supports escape sequences inside double-quoted strings.
🌐
PHP
php.net › manual › en › regexp.reference.escape.php
PHP: Escape sequences - Manual
"line break" is ill-defined: -- Windows uses CR+LF (\r\n) -- Linux LF (\n) -- OSX CR (\r) Little-known special character: \R in preg_* matches all three. preg_match( '/^\R$/', "match\nany\\n\rline\r\nending\r" ); // match any line endings ... Significantly updated version (with new $pat4 utilising \R properly, its results and comments): Note that there are (sometimes difficult to grasp at first glance) nuances of meaning and application of escape sequences like \r, \R and \v - none of them is perfect in all situations, but they are quite useful nevertheless.
🌐
PHPpot
phppot.com › php › php-escape-sequences
PHP Escape Sequences: Quotes, Newlines and Tabs - PHPpot
1 month ago - Most PHP escape sequences work inside double-quoted strings and heredoc strings. For example, \n creates a newline, \t creates a tab, and \" inserts a double quote. Single-quoted strings are much simpler.
🌐
CodeBasics
code-basics.com › programming › php course › escape equences
Escape equences | PHP | CodeBasics
Here we output one string with a name, then one string with a line feed and then another string. The program will display this on the screen: ... If we need to output \n as text (two separate printable characters), we can use the escape method we already know, adding another \ at the beginning. I.e., the sequence \\n will appear as the characters \ and n following each other. <?php print_r("Joffrey loves using \\n");Expand code
🌐
Code Boxx
code-boxx.com › home › 4 ways to add line break & new line in php
4 Ways To Add Line Break & New Line In PHP
November 14, 2023 - As to why there are 2 different characters – Welcome to the confusing cyber world, where everyone uses a different standard to represent a line break: ... Yep, this confusion really isn’t PHP’s fault. For us code ninjas, just remember that the safest for cross-platform compatibility is to use \r\n regardless. ... <?php $people = [ "John Doe", "Jane Doe", "June Doe", "Joy Doe", "Julius Doe" ]; foreach ($people as $person) { echo $person .
🌐
Delft Stack
delftstack.com › home › howto › php › php echo line break
How to Add Line Break in PHP | Delft Stack
February 2, 2024 - We use the nl2br() function and the newline \n escape sequence to insert the line break within the echo statement in PHP. The nl2br() function inserts the HTML line breaks before the new line, and the \n escape sequence denotes the new line.
🌐
Scaler
scaler.com › home › topics › how to create a new line in php?
How to Create a New Line in PHP? - Scaler Topics
April 14, 2024 - Use "\n" for New Lines: Stick to the standard \n newline character for representing line breaks within plain text strings. This is universally recognized and works across different operating systems (Unix, Windows, macOS). Consistent Line Endings: Be consistent with line endings throughout ...
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 63165419 › regular-expression-to-escape-line-breaks-in-a-string
php - Regular expression to escape line breaks in a string - Stack Overflow
July 30, 2020 - This is PHP code and even though the text happens to be python, its not a python related problem. Any text with newlines would do.
🌐
Uptimia
uptimia.com › home › questions › how to create a newline character in php?
How To Create A Newline Character In PHP?
November 2, 2024 - To create newline characters in PHP, use double quotes instead of single quotes. The correct syntax is "\r\n" rather than '\r\n'. Double quotes in PHP allow for the interpretation of escape ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-line-breaks-from-the-string-in-php
How to remove line breaks from the string in PHP? - GeeksforGeeks
July 11, 2025 - <?php // Declare a variable and initialize it // with strings containing <br> tag $text = "Geeks<br>For<br>Geeks"; // Display the string echo $text; echo "\n"; // Use str_replace() function to // remove <br> tag $text = str_replace("<br>", "", ...
🌐
GitHub
github.com › php › doc-en › issues › 4493
Line break escape sequence (\R) documentation is incomplete · Issue #4493 · php/doc-en
February 24, 2025 - The line break escape sequence (\R) seems to break certain UTF-8 characters, in this example ą. ... <?php $line = "Urządzenie Z-Wave nie odpowiedziało."; echo preg_replace("/\R+/", "\n", $line); echo preg_replace("/[\n\r]+/", "\n", $line);
Author: php
🌐
BUSoft Technologies
busofttech.com › home › php escape quotes how to do it right?
PHP Escape Quotes: How to Do It Right | BUSoftTech
March 10, 2026 - Escape sequences are started with the escaping character backslash (\) followed by the character which may be an alphanumeric or a special character. If it is an alphanumeric character, it gives special meaning to represent the line breaks \n, ...
🌐
Team Treehouse
teamtreehouse.com › community › echoing-line-breaks-with-php-fgets
Echoing line breaks with php fgets() (Example) | Treehouse Community
February 8, 2018 - The solution was to concatenate a <br />. ... Yea I hastily added the <? to my code forgetting the PHP when I realised the PHP formatting wasn't working ;) ... Similarly with the escape character...
🌐
Hacking with PHP
hackingwithphp.com › 2 › 6 › 2 › escape-sequences
Escape sequences – Hacking with PHP - Practical PHP
<?php $MyString = "This is an \"escaped\" string"; $MySingleString = 'This \'will\' work'; $MyNonVariable = "I have \$zilch in my pocket"; $MyNewline = "This ends with a line return\n"; $MyFile = "c:\\windows\\system32\\myfile.txt"; ?>