PHP Function to Convert Multiline String to HTML Paragraphs
PHP Function Code to Take Multiline String, Remove Empty Lines, Surround Remaining Lines with HTML Paragraph Tags <p> <\p>
Home
Short:
/* ---------------------------------------------------------- function takes multiple text [$text], removes empty lines and surround each remaining line with html paragraph tags <p> </p> ---------------------------------------------------------- */ function lineBreakToParagraphs($text, $numSpacesPadLeft = 0) { $spaces = str_repeat(" ", $numSpacesPadLeft); // normalize line breaks for both windows and unix environments $text = str_replace(array("\r\n", "\r"), "\n", $text); $arr = explode("\n", $text); // Remove empty strings from the array $arr = array_filter($arr, function($value) { return strlen(trim($value)) > 0; }); // if empty dont bother surround with <p></p> tags if( count($arr) == 0) { return ""; } // surround each arr elem with paragraph tags and join as string $out = $spaces . "<p>" . implode("</p>\n" . $spaces . "<p>", $arr) . "</p>"; return $out; }
source code home
S
H
A
R
E
Facebook
Twitter
LinkdIn
Reddit
Blogger
π Ώing
Email π§
π Multiple
Facebook
Twitter
Linkdin
Reddit
Blogger
Ping
Email
Share β