Page 1 of 1

Regular Expressions (removing extra white spaces)

Posted: Thu Dec 16, 2004 10:27 pm
by Rafael Almeida
Hello all!

What is the regular expression pattern to remove extra white spaces in the middle of strings (leaving just one)?

Posted: Thu Dec 16, 2004 11:52 pm
by rehfeld
replace 2 or more consequtive spaces w/ a single space

Code: Select all

<?php
$pattern = '/(\s{2,})/';
$subject = 'foo      bar     baz';

echo preg_replace($pattern, ' ', $subject);
?>