Skip to content

Instantly share code, notes, and snippets.

@bryanthuan
Created April 18, 2019 10:07
Show Gist options
  • Save bryanthuan/923fa32826f0be9bc0ae4ce3dbb0d406 to your computer and use it in GitHub Desktop.
Save bryanthuan/923fa32826f0be9bc0ae4ce3dbb0d406 to your computer and use it in GitHub Desktop.
Simple nl2p (wraps things in paragraph tags as opposed to line breaks - nl2br)
<?php
/**
* This wraps blocks of text (delimited by \n) in p tags (similar to nl2br)
* @author Scott Dover <[email protected]>
* @param str
* @return str
*/
function nl2p($string) {
/* Explode based on new-line */
$string_parts = explode("\n", $string);
/* Wrap each block in a p tag */
$string = '<p>' . implode('</p><p>', $string_parts) . '</p>';
/* Return the string with empty paragraphs removed */
return str_replace("<p></p>", '', $string);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment