Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Created June 2, 2016 20:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/470c09a7787eb8c5b694d9233c1ee1e6 to your computer and use it in GitHub Desktop.
Save kellenmace/470c09a7787eb8c5b694d9233c1ee1e6 to your computer and use it in GitHub Desktop.
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