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?
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
strposwill return 0 and your statement will evaluate to FALSE if the first character is\n, sostrstris a better choice. Alternatively you could change thestrposusage to: