Home >Backend Development >PHP Problem >How to determine whether there is a newline character in php
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.
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!