Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove line breaks with regex
<?php
$string = 'This is a new paragraph with
arbitrarily inserted newlines
at a certain width. To keep
these from turning into <br>s,
we want to replace them the right
way.
When there is a double new-line,
then we know that it really is a
new paragraph, and it is OK to keep
both newlines together.';
$content_without_line_breaks = preg_replace( '/(^|[^\n\r])[\r\n](?![\n\r])/', '$1 ', $string );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.