Remove excessive line returns

后端 未结 5 1577
野的像风
野的像风 2021-01-13 09:12

I am looking for something like trim() but for within the bounds of a string. Users sometimes put 2, 3, 4, or more line returns after they type, I need to sanitize this inp

5条回答
  •  佛祖请我去吃肉
    2021-01-13 09:50

    The following regular expression should remove multiple linebreaks while ignoring single line breaks, which are okay by your definition:

    ereg_replace("\n\n+", "\n\n", $string);
    

    You can test it with this PHP Regular Expression test tool, which is very handy (but as it seems not in perfect parity with PHP).

    [EDIT] Fixed the ' to ", as they didn't seem to work. Have to admit I just tested the regex in the web tool. ;)

提交回复
热议问题