title: How to replace one or two consecutive line breaks in a string?
body: <p>I'm developing a <a href="http://kottke.org/08/02/single-serving-sites" rel="nofollow noreferrer">single serving site</a> in PHP that simply displays messages that are posted by visitors (ideally surrounding the topic of the website).  Anyone can post up to three messages an hour.</p>
  <p>Since the website will only be one page, I'd like to control the vertical length of each message.  However, I do want to at least partially preserve line breaks in the original message.  A compromise would be to allow for two line breaks, but if there are more than two, then replace them with a total of two line breaks in a row.  Stack Overflow implements this.</p>
  <p>For example:</p>
  <pre><code>Porcupines\nare\n\n\n\nporcupiney.
  </code></pre>
  <p>would be changed to</p>
  <pre><code>Porcupines&lt;br /&gt;are&lt;br /&gt;&lt;br /&gt;porcupiney.
  </code></pre>
  <p>One tricky aspect of checking for line breaks is the possibility of their being collected and stored as <code>\r\n</code>, <code>\r</code>, or <code>\n</code>.  I thought about converting all line breaks to <code>&lt;br /&gt;</code>s using <code>nl2br()</code>, but that seemed unnecessary.</p>
  <p>My question: Using regular expressions in PHP (with functions like <code>preg_match()</code> and <code>preg_replace()</code>), how can I check for instances of more than two line breaks in a row (with or without blank space between them) and then change them to a total of two line breaks?</p>