- var_export: documentation ( source)
- preg_replace: documentation ( source)
<?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)
);