0

I have this field for entering car numbers, like AS 1234 10. I want a php code that can keep reduce the space to just one if in case user is to add more spaces. So like if he enters AS 1234 10, then it automatically truncates it to the original AS 1234 10.

Marvin
  • 13,325
  • 3
  • 51
  • 57
tenko19
  • 11
  • 1

2 Answers2

2

You can use a regular expression to replace multiple spaces by a single whitespace:

preg_replace("/[[:space:]]+/"," ",$input);

See a live demo.

Marvin
  • 13,325
  • 3
  • 51
  • 57
1

Try:

$text = 'AS  1234  3       1';
echo preg_replace('/\s+/', ' ', $text);
sudodev
  • 353
  • 1
  • 6
  • 18