3v4l.org

run code in 500+ PHP versions simultaneously
<?php $string = "My text has so much whitespace Plenty of spaces and tabs"; //Remove duplicate newlines $string = preg_replace("/[\n]*/", "\n", $string); //Preserves newlines while replacing the other whitspaces with single space var_export( preg_replace("/[ \t]*/", " ", $string) );

preferences:
64.8 ms | 2804 KiB | 5 Q