2009
03.13

Function

Removes redundant spaces within a string.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// :: JaZz phpFramework
// :: V1.0
// :: 13/03/2009 15:24:08
function remove_redundant_spaces($String = '')
{
  // :: Initalize Variables
  $Result = NULL;
  for ($i = 0; $i < strlen($String); $i++) {
    if (substr($String, $i, 1) != ' ') {
      $Result .= trim(substr($String, $i, 1));
    }
    else {
      while (substr($String, $i, 1) == ' ') {
        $i++;
      }
      $Result .= ' ';
      $i--;
    }
  }
  return $Result;
}


Usage

1
2
3
$String = 'This    string   contains    redundant    spaces';
$NewString = remove_redundant_spaces($String);
echo $NewString; // Output: This string conains redundant spaces


So long,
JaZz

No Comment.

Add Your Comment
*