Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your <code>ereg-replace()</code> solution is wrong because the <code>ereg/eregi</code> methods are deprecated. Your <code>preg_replace()</code> won't even compile, but if you add delimiters and set multiline mode, it will work fine:</p> <pre><code>$str = preg_replace('/^[ \t]*[\r\n]+/m', '', $str); </code></pre> <p>The <code>m</code> modifier allows <code>^</code> to match the beginning of a logical line rather than just the beginning of the whole string. The start-of-line anchor is necessary because without it the regex would match the newline at the end of every line, not just the blank ones. You don't need the end-of-line anchor (<code>$</code>) because you're actively matching the newline characters, but it doesn't hurt.</p> <p>The <a href="https://stackoverflow.com/a/709684/20938">accepted answer</a> gets the job done, but it's more complicated than it needs to be. The regex has to match either the beginning of the string (<code>^[\r\n]*</code>, multiline mode not set) or at least one newline (<code>[\r\n]+</code>), followed by at least one newline (<code>[\r\n]+</code>). So, in the special case of a string that starts with one or more blank lines, they'll be replaced with <em>one</em> blank line. I'm pretty sure that's not the desired outcome.</p> <p>But what it does most of the time is replace two or more consecutive newlines, along with any horizontal whitespace (spaces or tabs) that lies between them, with one linefeed. That's the intent, anyway. The author seems to expect <code>\s</code> to match just the space character (<code>\x20</code>), when in fact it matches any whitespace character. That's a very common mistake. The actual list varies from one regex flavor to the next, but at minimum you can expect <code>\s</code> to match whatever <code>[ \t\f\r\n]</code> matches.</p> <p>Actually, in PHP you have a better option:</p> <pre><code>$str = preg_replace('/^\h*\v+/m', '', $str); </code></pre> <p><code>\h</code> matches any horizontal whitespace character, and <code>\v</code> matches vertical whitespace.</p>
    singulars
    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.
    3. VO
      singulars
      1. This table or related slice is empty.
    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