Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New line to paragraph function

Tags:

regex

php

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.

like image 754
Jeremy Avatar asked Sep 13 '11 22:09

Jeremy


People also ask

Is a new line a new paragraph?

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.

How do you break a line in a paragraph?

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.

What is meant by nl2br ()?

The nl2br() function inserts HTML line breaks (<br> or <br />) in front of each newline (\n) in a string.

How do you start a new line in Word?

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 .


2 Answers

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 ?> 
like image 93
Jonathan Avatar answered Sep 22 '22 02:09

Jonathan


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>';  } 
like image 42
Laurent Avatar answered Sep 19 '22 02:09

Laurent