Last active
August 5, 2019 09:17
-
-
Save giventofly/86ffe220ba6bac6816b968353c1dbb47 to your computer and use it in GitHub Desktop.
Simple nl2p (wraps things in paragraph tags as opposed to line breaks - nl2br)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
/**** my nl2p ***************/ | |
/* wrap text ending with \n with <p> </p> tags */ | |
function wrap_nl($string){ | |
//remove tabs/nl | |
$prep0 = str_replace(array("\r\n" , "\n\r") , "\n" , utf8_decode($string)); | |
$prep1 = str_replace("\r" , "\n" , $prep0); | |
//remove empty spaces | |
$prep2 = preg_replace(array('/\n\s+/' , '/\s+\n/') , "\n" , trim($prep1)); | |
$result = '<p>'.str_replace("\n", "</p>\n<p>", $prep2).'</p>'; | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment