Skip to content
PHP Logo

What is the issue with using '\n' in the split function in PHP and how can it be resolved?

Using '\n' in the split function in PHP can cause issues because PHP interprets '\n' as a literal string rather than a new line character. To resolve this, you can use the PHP escape sequence for a new line, which is "\n".

php
$string = "Hello\nWorld";
$lines = preg_split('/\n/', $string);
print_r($lines);