Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try a regular expression to limit sequential newlines before passing things into nl2br:</p> <pre><code>$clean = preg_replace('/\n{4,}/', '\n', preg_replace('/\r/', '', $dirty)); </code></pre> <p>For the uninitiated:</p> <ul> <li>\r is a Carriage Return, which we can safely strip before calling nl2br</li> <li>\n is a Newline</li> <li>{4,} means "repeat the last character four or more times</li> </ul> <p>So, any sequence of four or more newlines in a row will be reduced to one.</p> <p>Unfortunately this is easily defeated. A user can simply come by and hit enter + space + enter + space + enter + space...</p> <p>It's a start, though. Let's make it a bit more idiot-proof.</p> <pre><code>$dirty = preg_replace('/\r/', '', $dirty); $clean = preg_replace('/\n{4,}/', '\n', preg_replace('/^\s+$/m', '', $dirty)); </code></pre> <p>Now we're also:</p> <ul> <li>Setting the "m" modifier so...</li> <li>The ^ means "the beginning of a line" (without "m" it means the beginning of the entire string)</li> <li>The $ means "the end of a line" (without "m" it means the end of the entire string)</li> <li>\s+ means "any space character, repeating"</li> </ul> <p>So, now it removes \r, then changes lines that are nothing but spaces to being empty, then it limits empty lines.</p> <p>Beware, this may break formatting, depending on what markup you're using. Certain markup engines can operate on lines that are pure-whitespace and do smart things with them, such as the markup engine used here at SO.</p> <p>Have you considered banning jerks? It's sometimes effective. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload