Replace multiple newlines, tabs, and spaces

前端 未结 10 1870
情深已故
情深已故 2020-11-27 05:56

I want to replace multiple newline characters with one newline character, and multiple spaces with a single space.

I tried preg_replace(\"/\\n\\n+/\", \"\\n\",

10条回答
  •  难免孤独
    2020-11-27 06:52

    You need the multiline modifier to match multiple lines:

    preg_replace("/PATTERN/m", "REPLACE", $text);
    

    Also in your example you seem to be replacing 2+ newlines with exactly 2, which isn't what your question indicates.

提交回复
热议问题