Be careful with John's regular expression, if all you want to strip out is spaces. The regular expression above strips all whitespace. Example:
Code:
<?php
$string = "This is a test\r\nTesting\r\nWhere are mye linebreaks?\r\n";
echo $string; // With line break
$string = preg_replace('/\s{2,}/', ' ', trim($string));
echo $string; // Stripped
?>
Output:
Code:
This is a test
Testing
Where are mye linebreaks?
This is a test Testing Where are mye linebreaks?
You could use this just to remove more than one space, preserve Unix style line breaks and keep spacing between words:
Code:
$string = preg_replace('/ {2,}/', ' ', trim($string));
Example:
Code:
<?php
$string = "A lot of spaces here\r\nPreserves Unix style line breaks?\r\n";
echo $string; // With Spaces
$string = preg_replace('/ {2,}/', ' ', trim($string));
echo $string; // Stripped and preserves line breaks
?>
Output:
Code:
A lot of spaces here
Preserves Unix style line breaks?
A lot of spaces here
Preserves Unix style line breaks?
Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum