$string = \"My text has so much whitespace
Plenty of spaces and tabs\";
echo preg_replace(\"/\\s\\s+/\", \" \", $string);
Alternative approach:
echo preg_replace_callback("/\s+/", function ($match) {
$result = array();
$prev = null;
foreach (str_split($match[0], 1) as $char) {
if ($prev === null || $char != $prev) {
$result[] = $char;
}
$prev = $char;
}
return implode('', $result);
}, $string);
Output:
My text has so much whitespace
Plenty of spaces and tabs
Edit: Readded this for it being a different approach. It's probably not what's asked for, but it will at least not merge groups of different whitespace (e.g. space, tab, tab, space, nl, nl, space, space
would become space, tab, space, nl, space
).
//Newline and tab space to single space
$from_mysql = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $from_mysql);
// Multiple spaces to single space ( using regular expression)
$from_mysql = ereg_replace(" {2,}", ' ',$from_mysql);
// Replaces 2 or more spaces with a single space, {2,} indicates that you are looking for 2 or more than 2 spaces in a string.
Had the same problem when passing echoed data from PHP to Javascript (formatted as JSON). The string was peppered with useless \r\n and \t characters that are neither required nor displayed on the page.
The solution i ended up using is another way of echoing. That saves a lot of server resources compared to preg_replace (as it is suggested by other people here).
Here the before and after in comparison:
Before:
echo '
<div>
Example
Example
</div>
';
Output:
<div>\r\n\r\n\tExample\r\n\tExample\r\n\r\n</div>
After:
echo
'<div>',
'Example',
'Example',
'</div>';
Output:
<div>ExampleExample</div>
(Yes, you can concatenate echo not only with dots, but also with comma.)
First, I'd like to point out that new lines can be either \r, \n, or \r\n depending on the operating system.
My solution:
echo preg_replace('/[ \t]+/', ' ', preg_replace('/[\r\n]+/', "\n", $string));
Which could be separated into 2 lines if necessary:
$string = preg_replace('/[\r\n]+/', "\n", $string);
echo preg_replace('/[ \t]+/', ' ', $string);
Update:
An even better solutions would be this one:
echo preg_replace('/[ \t]+/', ' ', preg_replace('/\s*$^\s*/m', "\n", $string));
Or:
$string = preg_replace('/\s*$^\s*/m', "\n", $string);
echo preg_replace('/[ \t]+/', ' ', $string);
I've changed the regular expression that makes multiple lines breaks into a single better. It uses the "m" modifier (which makes ^ and $ match the start and end of new lines) and removes any \s (space, tab, new line, line break) characters that are a the end of a string and the beginning of the next. This solve the problem of empty lines that have nothing but spaces. With my previous example, if a line was filled with spaces, it would have skipped an extra line.