I have this interesting function that I'm using to create new lines into paragraphs. I'm using it instead of the nl2br()
function, as it outputs better formatted text.
function nl2p($string, $line_breaks = true, $xml = true) { $string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string); // It is conceivable that people might still want single line-breaks // without breaking into a new paragraph. if ($line_breaks == true) return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>'; else return '<p>'.preg_replace( array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"), array("</p>\n<p>", "</p>\n<p>", '<br'.($xml == true ? ' /' : '').'>'), trim($string)).'</p>';
}
The problem is that whenever I try to create a single line break, it inadvertently removes the first character of the paragraph below it. I'm not familiar enough with regex to understand what is causing the problem.
When a line break is inserted the cursor moves down a single line, which is different from the paragraph which ends the paragraph and starts a new one.
Line Breaks - Hold Shift and Press Enter When you hold Shift and press Enter a line break tag is inserted ( <br /> ) and the text entered after the line break will appear on the next line down.
The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.
Move the text cursor to where you want the new line to begin, press the Enter key, hold down the Shift key, and then press Enter again. You can continue to press Shift + Enter to move to each new line, and when ready to move to the next paragraph, press Enter .
Here is another approach that doesn't use regular expressions. Note, this function will remove any single line-breaks.
function nl2p($string) { $paragraphs = ''; foreach (explode("\n", $string) as $line) { if (trim($line)) { $paragraphs .= '<p>' . $line . '</p>'; } } return $paragraphs; }
If you only need to do this once in your app and don't want to create a function, it can easily be done inline:
<?php foreach (explode("\n", $string) as $line): ?> <?php if (trim($line)): ?> <p><?=$line?></p> <?php endif ?> <?php endforeach ?>
The problem is with your match for single line breaks. It matches the last character before the line break and the first after. Then you replace the match with <br>
, so you lose those characters as well. You need to keep them in the replacement.
Try this:
function nl2p($string, $line_breaks = true, $xml = true) { $string = str_replace(array('<p>', '</p>', '<br>', '<br />'), '', $string); // It is conceivable that people might still want single line-breaks // without breaking into a new paragraph. if ($line_breaks == true) return '<p>'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^<])/i"), array("</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>'; else return '<p>'.preg_replace( array("/([\n]{2,})/i", "/([\r\n]{3,})/i","/([^>])\n([^<])/i"), array("</p>\n<p>", "</p>\n<p>", '$1<br'.($xml == true ? ' /' : '').'>$2'), trim($string)).'</p>'; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With