Remove excessive line returns

后端 未结 5 1537
野的像风
野的像风 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:47

    Finally managed to get it, needs preg so you are using the PCRE version in php, and also needs a \n\n replacement string, in order to not wipe all line endings but one:

      $body = preg_replace("/\n\n+/", "\n\n", $body);
    

    Thanks for getting me on the right track.

提交回复
热议问题