Php find new line in string

This doesn't work:

$string = 'Hello
    world';

if[strpos[$string, '\n']] {
    echo 'New line break found';
}
else {
    echo 'not found';
}

Obviously because the string doesn't have the "\n" character in it. But how else can I check to see if there is a line break that is the result of the user pressing enter in a form field?

animuson

52.7k28 gold badges139 silver badges145 bronze badges

asked Feb 24, 2012 at 19:52

6

Your existing test doesn't work because you don't use double-quotes around your line break character ['\n']. Change it to:

if[strstr[$string, "\n"]] {

Or, if you want cross-operating system compatibility:

if[strstr[$string, PHP_EOL]] {

Also note that strpos will return 0 and your statement will evaluate to FALSE if the first character is \n, so strstr is a better choice. Alternatively you could change the strpos usage to:

if[strpos[$string, "\n"] !== FALSE] {
  echo 'New line break found';
}
else {
  echo 'not found';
}

answered Feb 24, 2012 at 19:55

rdlowreyrdlowrey

39.3k9 gold badges76 silver badges98 bronze badges

4

line break is \r\n on windows and on UNIX machines it is \n. so its search for PHP_EOL instead of "\n" for cross-OS compatibility, or search for both "\r\n" and "\n".

answered Feb 24, 2012 at 19:59

Mouna CheikhnaMouna Cheikhna

37.7k10 gold badges47 silver badges68 bronze badges

1

The most reliable way to detect new line in the text that may come from different operating systems is:

preg_match['/\R/', $string]

\R comes from PCRE and it is the same as: [?>\r\n|\n|\r|\f|\x0b|\x85|\x{2028}|\x{2029}]

The suggested answer to check PHP_EOL

if[strstr[$string, PHP_EOL]] {

will not work if you are for example on Windows sytem and checking file created in Unix system.

answered Nov 8, 2020 at 16:39

sprutexsprutex

6518 silver badges8 bronze badges

Topic: PHP / MySQLPrev|Next

Answer: Use the Newline Characters '\n' or '\r\n'

You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br[] function which inserts HTML line breaks before all newlines in a string.

Let's take a look at the following example to understand how it basically works:

Note: The character \n writes a newline in UNIX while for Windows there is the two character sequence: \r\n. To be on safe side use the \r\n instead.

Related FAQ

Here are some more FAQ related to this topic:

  • How to combine two strings in PHP
  • How to remove white space from a string in PHP
  • How to write comments in PHP

[PHP 4, PHP 5, PHP 7, PHP 8]

nl2brInserts HTML line breaks before all newlines in a string

Description

nl2br[string $string, bool $use_xhtml = true]: string

Parameters

string

The input string.

use_xhtml

Whether to use XHTML compatible line breaks or not.

Return Values

Returns the altered string.

Examples

Example #1 Using nl2br[]

Collette

2 years ago

Let's say a form text field does contain:
test line number 1
test line number 2

To remove all the line breaks and split the input into an array:

$input_from_text_field = preg_replace['#[\r\n]#','',nl2br[$input_from_text_field,false]];
$lines = explode["
",$input_from_text_field];

$lines will now be an array where $lines[0] = "test line number 1" and so on.

buzanits at gmail dot com

4 years ago

If you write code that is to run in browser AND on the shell, this function could be useful. It uses PHP_EOL or
depending on the platform it runs:

leo dot mauro dot desenv at gmail dot com

7 years ago

I test empirically this function nl2br and nl2br2 [create by ngkongs at gmail dot com].
Both work nice with different ASCII chars for linebreak, but the function nl2br2 is faster than nl2br.

nl2br2 ~ 0.0000309944153 s
nl2br  ~ 0.0011141300201 s

The function nl2br2:

Justinas M.

6 years ago

This is example with "\R" regex token which matches any unicode newline character.
"u" flag treate strings as UTF-16. Which is optional, depending on your use case.



NOTE:
preg_replace versions are much slower than using str_replace version or built-in nl2br.
Check out pcre.backtrack_limit php.ini setting for information about PCRE limit. It's good to know.

Some PHP7 benchmarks:


# nl2br
## Time: 0.02895712852478 s

# nl2br_str
## Time: 0.027923107147217 s

# nl2br_preg_R
## Time: 0.13350105285645 s

# nl2br_preg_rnnr
## Time: 0.14213299751282 s

chad at nobodyfamous dot ca

5 years ago

I just spent a whole day trying to figure out why my textarea $_POST was not getting
tags added by nl2br[].  So for others

returns newlines as  and so nl2br will miss them.

rather use and newlines will remain intact so nl2br will pick them up.

blacknine313 at gmail dot com

14 years ago

After a recent post at the forums on Dev Shed, I noticed that it isn't mentioned, so I will mention it.

nl2br returns pure HTML, so it should be after PHP anti-HTML functions [ such as strip_tags and htmlspecialchars ].

darenschwenke at yahoo dot com

8 years ago

This one works with br tags having attributes, in any case,
closed or  not closed, and does not double linefeeds



I combine this with strip_tags[] for dead simple "contenteditable" fields allowing only text and linefeeds.

j dot mons54 at gmail dot com

10 years ago

for bbcode :

hyponiq at gmail dot com

15 years ago

On the contrary, mark at dreamjunky.comno-spam, this function is rightfully named.  Allow me to explain.  Although it does re-add the line break, it does so in an attempt to stay standards-compliant with the W3C recommendations for code format.

According to said recommendations, a new line character must follow a line break tag.  In this situation, the new line is not removed, but a break tag is added for proper browser display where a paragraph isn't necessary or wanted.

How do you find a new line in a string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

Does \n work in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code.

What is Br in PHP?

Using Line Breaks as in HTML: The
tag in HTML is used to give the single line break. It is an empty tag, so it does not contain an end tag. Syntax:
Example: PHP.

How do you check line breaks?

To check whether a JavaScript string contains a line break, we can use the JavaScript regex's exec method. We call /\r|\n/. exec with text to return the matches of newline characters in text .

Chủ Đề