Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's one possible approach to solve the problem given in the original question (<a href="http://ideone.com/xqUBLw" rel="nofollow">demo</a>): one can remove all the newlines within the double quoted strings (but only those!) by...</p> <pre><code>preg_replace('#\\n(?=[^"]*"[^"]*(?:"[^"]*"[^"]*)*$)#' , ' ', $help); </code></pre> <p>The core idea is very simple: for each end of line symbol, we make sure that it's followed by (<code>DQM</code> = <code>"</code>)...</p> <ul> <li>any number of non-DQM symbols, then...</li> <li>exactly one DQM, then...</li> <li>any number of non-DQM, then...</li> <li>any number of <code>single DQM - any number of non-DQM - single DQM - any number of non-DQM</code> combos, then...</li> <li>the end of string.</li> </ul> <p>For properly formed string, this will result in collecting endlines lying in between double quotation marks, as asked.</p> <p>There's a caveat to this approach, though. Obviously we won't be able to correct the line if it has an odd number of DQMs (even more, it will work incorrectly in this case). That's easy to check, just count the DQMs in the string. BTW, desired behaviour is a bit unclearly for such strings:</p> <pre><code>"should "we replace" endline here ? </code></pre> <p>In theory, it still can be fixed a bit, by using look-behind instead of look-ahead, something like this...</p> <pre><code>preg_replace('#(?&lt;=^(?:[^"]*"[^"]*")*[^"]*"[^"]*)\\n#' , ' ', $help); </code></pre> <p>... but in practice, one can't (still) use look-behind expressions of variable length in PHP. So you have to resort to parsing this string in this case.</p> <p>If this consideration is not relevant in your case, though, the approach shown might be helpful, I suppose.</p>
 

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