title: How can I remove last comma and all spaces from string, then sanitize it using PHP?
body: <p>I want to get the string from input field, then format and sanitize it.</p>
  <p>The strings I want to get is natural number separated by commas without any spaces.
  First I want to remove all spaces and last comma.
  I mean if the formatted string is not match what I want, I want it to return empty string.</p>
  <pre><code>//OK examples(without any spaces)
  1,2,123,45,7
  132,1,555,678
  </code></pre>
  <pre><code>//NG examples
  aaa,111,2365
  1,2,123,45,7,
  -1,2,123,45,7,,,
  1, 2, 123, 45,  7
  </code></pre>
  <p>First I want to remove spaces and last comma
  1,  235, 146,  =&gt; 1,235,146</p>
  <p>I tried code below</p>
  <pre><code>$input = str_replace(' ', '', $input);
  rtrim($input, ',');
  if (preg_match('/^\d(?:,\d+)*$/', $input)) {
      return $input;
  }
  return '';
  </code></pre>
  <p>This one, if the string has spaces after last comma, it returns empty string.</p>
  <pre><code>1,2,123,45,7,   =&gt; //returns empty string.
  </code></pre>
  <p>I want to format this to '1,2,123,45,7'.</p>
  <p>Sorry for my messy explanation...</p>