Saturday 15 August 2015

php - Finding a newline not immediately followed by a newline -


(In PHP ...) I am trying to get a block of text and remove all hard line breaks so that It can be re-wrapped for a specific line length using Wordwrap ().

However, after having many paragraphs in it, I'm assuming that I want a way to remove a new line, but not two lines.

If this is correct, how can I say \ n but \ n \ n Not in a regular expression? Or am I coming wrongly?

Try:

  Preg_replace ('! (? & Lt ;; \ N) \ n (?! \ N)! ',' ', $ Input);  

See how it works.

This removes any new lines that are not before or after a new line if you have this combo then you have to do otherwise:

  This is something The text is \ n \ n  

will not match first (not according to Newline New Line) as it will be done after a new line, but the second will be because it is a new line (But it's already one).

Alternatively you can:

  preg_replace_callback ('! \ N +!', 'Replace_newline', $ input); Replace function_new ($ matches) {Return strollen ($ matches [0]) == 1? '': $ Matches [0]; }  

In addition to this I have also assumed that you are just new lines and not even \ r as well. Either the solution can be adjusted for it.


No comments:

Post a Comment