Home > Backend Development > PHP Problem > How to determine whether there is a newline character in php

How to determine whether there is a newline character in php

藏色散人
Release: 2023-03-17 11:24:02
Original
2223 people have browsed it

php method to determine whether there is a newline character: 1. Create a PHP sample file; 2. Define a string containing a newline character; 3. Use "if(strstr($string, "\n") ) {echo 'New line break found';}else {echo 'not found';}" can be used to determine whether there is a newline character.

How to determine whether there is a newline character in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to determine if there is a newline character in php?

The code is as follows:

$string = 'Hello
    world';

if(strstr($string, "\n")) {
    echo 'New line break found';
}
else {
    echo 'not found';
}
Copy after login

Or, if you want cross-operating system compatibility

The fourth line can be changed to:

if(strstr($string, PHP_EOL)) {
Copy after login

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

if(strpos($string, "\n") !== FALSE) {
  echo 'New line break found';
}
else {
  echo 'not found';
}
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether there is a newline character in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template