preg_replace removes multiple “\n”s

2019-07-13 10:18发布

How do I remove multiple occurrences of \n from the sample below and replace with just one occurrence of \n?

Basically I just want to remove multiple line breaks and replace them with just one line break.

\n\n\n\n\n\n\n\n \n\n \n\n\n\n\n\n\n    \n\n     \n\n     \n\n    \n\n    \n\n     \n\n     \n\n     \n     \n     \n     \n     \n     \n     \n\n     \n\n     \n\n     \nEDITION:  U.S.\n\n \nINTERNATIONAL\n\n     \nMÉXICO\n\n     \n\n     \nSet edition preference\n\n     \n\n     \n\n     \n\n     \nSign up\n\n     \nLog in\n\n     \n\n \n\n     \n\n     \n\n     \n\n\n\n\n\n\n\n\n\n\n\n     \n\n     \n\n\n \n\n \n\n     \n\n     \n\n    \n\n    \n\n     \n\n     \nHome\n\n     \nVideo\n\n     \nNewsPulse\n\n \nU.S.\n\n     \nWorld\n\n     \nPolitics\n\n     \nJustice\n\n     \nEntertainment\n\n     \nTech\n\n     \nHealth\n\n     \nLiving\n\n     \nTravel\n\n \nOpinion\n\n     \niReport\n\n     \nMoney\n\n     \nSports\n\n     \n\n    \n\n\n\n\n\n \n\n\n\n\n\n\n\n \n\n\n\n \n\n\n\n\n\n\n \n\nupdated 10:02 a.m.EDT, Fri June 3, 2011\n\n\n\n\n\n \n\n\n\n\n\nDr. Jack Kevorkian dead at 83\n\n\n\n\n\n\nThe Michigan pathologist who put assisted suicide on the world\'s medical ethics stage, apparently died of a blood clot, according to his attorney. FULL STORY

7条回答
Summer. ? 凉城
2楼-- · 2019-07-13 11:15

Try:

$newstr = preg_replace("/\r\n\r\n|\r\r|\n\n/", "..", $str);
查看更多
登录 后发表回答