Regular expression x remove \n\r to text
Moderator: General Moderators
Regular expression x remove \n\r to text
Hi, I must transform html template into txt template
but I have any problem to make this regular expression:
remove all \n{2,}
for remove more than 2 occurencies of \n or \n\r
?
tx
but I have any problem to make this regular expression:
remove all \n{2,}
for remove more than 2 occurencies of \n or \n\r
?
tx
Code: Select all
^.*$You should download regex coach:
http://weitz.de/regex-coach/#install
That will allow you to test your expressions on your desktop.
This site is a great resource too:
http://regular-expressions.info
Hi, thks for infoneophyte wrote:I believe this expression would match any character on a line. But not line breaks.Code: Select all
^.*$
You should download regex coach:
http://weitz.de/regex-coach/#install
That will allow you to test your expressions on your desktop.
This site is a great resource too:
http://regular-expressions.info
But, I need to replace more than 2 > \n\r in a single \n
tx
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Would that be, turn all \r\n into just \n? \n\r isn't used anywhere as far as I know.
If that's not what you meant, you'll need to give some examples as I don't seem to understand what you meant 
Code: Select all
preg_replace("~\r(?=\n)~", "\n", $text);If I'm not mistaken your regex is turning \r\n into \n\n. Remember that lookarounds don't 'consume' text.d11wtq wrote:Would that be, turn all \r\n into just \n?
Code: Select all
preg_replace("~\r(?=\n)~", "\n", $text);
Code: Select all
// Try:
preg_replace('~\r(?=\n)~', '', $text);txd11wtq wrote:Would that be, turn all \r\n into just \n? \n\r isn't used anywhere as far as I know.
If that's not what you meant, you'll need to give some examples as I don't seem to understand what you meantCode: Select all
preg_replace("~\r(?=\n)~", "\n", $text);
we must replace all >2 occurencies of \r OR \n OR \r\n OR \n\r with a single \n
tx
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
preg_replace("~[\r\n]{2,}~", "\n", $text);That regex would also turn a single occurrence of \r\n into \n.
Credits for the code below goes to the WordPress coders. These lines can be found in the auto_p() function:
Credits for the code below goes to the WordPress coders. These lines can be found in the auto_p() function:
Code: Select all
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates