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

How to determine whether there is a newline character in php

藏色散人
藏色散人Original
2022-11-04 09:56:201918browse

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';
}

Or, if you want cross-operating system compatibility

The fourth line can be changed to:

if(strstr($string, PHP_EOL)) {

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';
}

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!

Statement:
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