replacing \n with new line
You can skip parts of the string using the backtracking control verb (*SKIP)
:
$result = preg_replace('~\\\\.(*SKIP)(?<=n)~', "\n", $source);
demo
(*SKIP)
skips positions of the string already reached by the subpattern before it when the subpattern after it fails.(?<=n)
is a lookbehind assertion and means preceded by n. When it fails, the regex engine jumps after the escaped character position (the position defined by (*SKIP)
) for a new attempt, instead of testing the next position (the position after the backslash).
You can also do the same using strtr
:
$result = strtr($source, ['\\\\' => '\\\\', '\\n' => "\n"]);
(since there's a replacement defined for two consecutive backslashes, the second backslash can't be used for an eventual following n
.)
PHP - how to create a newline character?
Only double quoted strings interpret the escape sequences \r
and \n
as '0x0D' and '0x0A' respectively, so you want:
"\r\n"
Single quoted strings, on the other hand, only know the escape sequences \\
and \'
.
So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n"
or using chr
function chr(0x0D).chr(0x0A)
), the only other way to have a line break within a single quoted string is to literally type it with your editor:
$s = 'some text before the line break
some text after';
Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n
for example).
PHP Linefeeds (\n) Not Working
Use double quotes. "test\n"
will work just fine (Or, use 'test' . PHP_EOL
).
If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:
http://php.net/manual/en/language.types.string.php
New line (\n) in PHP is not working
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.
Why do I get \n instead of a new line getting data from AJAX?
Store it as it is but escape first with real_escape_string
real_escape_string
converts what is a newline into the 4 character string '\n\r'
$text = $mysqli->real_escape_string($text);
PHP: trying to create a new line with \n
Newlines in HTML are expressed through <br>
, not through \n
.
Using \n
in PHP creates a newline in the source code, and HTML source code layout is unconnected to HTML screen layout.
Writing a new line to file in PHP (line feed)
Replace '\n'
with "\n"
. The escape sequence is not recognized when you use '
.
See the manual.
For the question of how to write line endings, see the note here. Basically, different operating systems have different conventions for line endings. Windows uses "\r\n", unix based operating systems use "\n". You should stick to one convention (I'd chose "\n") and open your file in binary mode (fopen
should get "wb", not "w").
How to replace `\n` with new line when print as pre in HTML?
If it's going in a pre
, then you shouldn't need to replace the \n
since that is the newline.
document.querySelector("pre").textContent = "File version: 5\n\nstart: 1410355285955(2014/09/10 11:58:05.955)\nend: 141090402750(2014/09/10 12:00:02.750)\nUEID: 301660031\nCNC: 118130\nVendor: 75306\nModel: 807182328\nOS: 1549568403\nPlatform: 1\nCarrier: -1337093915\nReported Networks: 0\n\nNum domains: 1\n Domain 276975141\n Num tags: 1\n -2032188703\n Num hostedApps: 2\n HostedApp: -2097733624, HostedVersion 300009300500000, ..."
<pre></pre>
PHP newline not working in text file
$numberNewline = $number . "\n";
fwrite($file, $numberNewline);
Try this
Writing TXT File with PHP, Want to Add an Actual Line Break
Sounds to me like you might be using single quotes, i.e. '\n'
rather than "\n"
.
If you wanted to continue with a single quotes bias (as you should!), two options:
file_put_contents('/path/to/file.txt', 'Hello friend!
This will appear on a new line.
As will this');
// or
file_put_contents('/path/to/file.txt', 'Hello friend!'."\n".'This will appear on a new line.'."\n".'As will this');
Related Topics
PHP Jquery Ajax Call Throws Net::Err_Empty_Response
How to Check If a MySQL Query Using the Legacy API Was Successful
Create PHP Array from MySQL Column
How to Select Multiple Files for Upload
PHP Import Excel into Database (Xls & Xlsx)
Inheritance of Static Members in PHP
How to Get User Image with Twitter API 1.1
A _Construct on an Eloquent Laravel Model
Get a Coupon Code via Url and Apply It in Woocommerce Checkout Page
PHP - Implement Logging Mechanism to File in Several Classes
Set Character Set Using MySQLi
Symfony 2 Load Different Template Depending on User Agent Properties
Using Prepared Statement, How I Return the Id of the Inserted Row
Work-Around for PHP5's Pdo Rowcount MySQL Issue
What's the Prefix for Binary in PHP
What Is the Mm/Dd/Yyyy Regular Expression and How to Use It in PHP