Cleaning up spaces and punctuations

The users of my web site don’t know where to put spaces around punctuation symbols. For example, after a period, comma or a semicolon there should be a whitespace but not after decimals or thousands-separators in numbers.

Does anyone have handy a regular expression that cleans up the text in this way?

Quick-n-dirty. :slight_smile:


<?php
function punctuate($string, $chars = ',.;'){
  return preg_replace(
    sprintf(
      '~([&#37;s])(?![0-9\\s])~',
      preg_quote($chars)
    ),
    '$1 ',
    $string
  );
}

echo punctuate('12.00sdg,df.g,sd.g,d3,5664.00.Year 1900.');
#12.00sdg, df. g, sd. g, d3,5664.00. Year 1900.
?>

Thanks.

Could you please modify it so that it deletes any whitespaces before the punctuation mark if they exist? I have to admit I’m lost when it comes to regular expressions and your function looks even stranger, with no forward slashes for delimiting patterns and sprintf() which I’ve never used. :expressionless: I promise I’ll get a good regex book. :slight_smile:

Edit: uhhh never mind, I got it. Thanks. :slight_smile: