Re: Differentiate Line breaks and Paragraphs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2 Nov 2012, at 19:32, Simon Dániel <simondani91@xxxxxxxxx> wrote:

> Is there a way to make difference between line breaks and paragraphs, when
> I save a text to a database from a user input? The new line characters (\n,
> \r) are not too consequent to determine when did the user only one new line
> or more new lines. (Some clients make only \n or \r, some others make \n\r
> or \r\n, etc.)
> 
> What I would like to do is, to format the given text into paragraphs (with
> <p> tag), not just insert a batch of break line (<br />) tags.
> Of course, I want to retain <br /> tags as well, in case of the user wrote
> only one new line character.


Something like this should work…

<?php
  // Let's say you have $str as the text, and
  // assuming that all line breaks are \r\n and
  // not just \n. If not I recommend replacing
  // \r\n with \n first and removing all \r's
  // from the code below.
  $new_str = nl2br('<p>'.str_replace($str, '\r\n\r\n', '</p><p>').'</p>');
?>

Untested but that should do what you're looking for.

-Stuart

-- 
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux